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. 

No comments:

Post a Comment