Monday, September 26, 2016

SUGAR CRM: Retrieving Relationships Programatically in Logic Hooks

1. Create Logic Hook Trigger in your corresponding module (Assume this is a before_save trigger)
 
$hook_array['before_save'][] = 
 Array(     
       1,     
       'Some Description',     
       'custom/modules/<your_module>/<your_file_name>.php', 
       '<your_class_name>',     
       '<your_method_name>'
 );
   
2. Create class and logic

class <your_class_name>
{ 
   public function <your_method_name>($bean, $event, $args) 
   {         
      try         
      { 
         if (isset($bean->fetched_row['id'])) 
         {
             // Assume we're using documents
             $bean->load_relationship("documents");         
             foreach($bean->documents->getBeans() as $doc)
             {                     
                $GLOBALS['log']->fatal($doc->category_id);
             } 
         } 
      }              
      catch (Exception $ex) 
      {             
          $GLOBALS['log']->fatal($ex->getTraceAsString()); 
      } 
   } 
}
 
NOTE: Make sure you're retrieving the correct relationship name.
A common mistake is to grab the relationship name from Studio under
Admin. Try to obtain the relationship name from 
cache\modules\<your_module>\<your_module>vardefs.php

Finally, get the array name value not the relationship value:
'documents' => array  
(   
   'name' => 'documents', 
   'type' => 'link',   
   'relationship' => 'documents_opportunities', 
   'source' => 'non-db',   
   'vname' => 'LBL_DOCUMENTS_SUBPANEL_TITLE',
 ),
 
In this case we need "documents" and not "documents_opportunities"
for the relationship name. 

Wednesday, September 21, 2016

JavaScript: Replacing All Instances of a Character

Let's assume that we want to replace all hyphens with blank spaces.

We can either loop through the string character by character until we replace all hyphens,
or we can create a regular expression as follows:

var myStr = 'This-is-a-test';
var replacedString = myStr.replace(/-/g, ' ');
Where g represents a global match.

Friday, September 16, 2016

JavaScript: Creating Objects

function myObject()
{
    this.myField1= true;
    this.myField2= new Array();
}

var obj1= new myObject();
obj1.myField1 = false;
obj1.myField2.push('value');

JavaScript: Using a Map Data Structure

var myMap = {};

myMap['key1'] = 'value1';
myMap['key2'] = 'value2';

for (var key in myMap) 
{
   if(myMap[key] == 'value1'){...}
}

jQuery: Replacing All Blank Spaces in a String

var myVar= j$( "#myId option:selected" ).text().replace(/\s+/g, " ");

jQuery: Getting Selected Option

j$( "#myId option:selected" ).text();

jQuery: Traversing All Rows Except Table Headers

j$('#lineItemsTable > tbody > tr').not('thead tr').each(function()
{
     // logic here
});