After Retrieve Logic Hook is running multiple times in a loop

Hi,

How to prevent after retrieve logic hook from executing more than once.
I tried session variables, static variable for locking but of nothing happened.

Thanks.
  • Thanks Usman Saeed,

    $hook_array['after_retrieve'] = Array(); 
    $hook_array['after_retrieve'][] = Array('1','Calculate Age','custom/modules/Leads/AgeCalculatorOnRetrieve.php','AgeCalculatorOnRetrieve','calculate_age_on_retrieve');


    <?php
    require_once('modules/Leads/Lead.php');
    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    class AgeCalculatorOnRetrieve
    {
    function calculate_age_on_retrieve($bean, $event, $arguments)
    {
      $GLOBALS['log']->fatal('AgeCalculatorOnRetrieve::calculate_age_on_retrieve()');
     
      if(isset($bean->birthdate) && !empty($bean->birthdate)){  
       require_once('include/TimeDate.php');
       $my_timedate = new TimeDate();  
       $date_of_birth = $bean->birthdate;
       $birthdate = $my_timedate->to_db_date($date_of_birth);  
       $age = floor( (strtotime(date('Y-m-d')) - strtotime($birthdate)) / 31556926);        
      
       $GLOBALS['log']->fatal("Age: ".$age);
      
       /*******************************************/
       //Bean Save Causing Problems, "Resolve Conflict".
       // $bean->age_c = $age;     
    // $bean->save();

       $sql = "UPDATE leads_cstm SET age_c='".$age."' WHERE id_c='".$bean->id."' ";  
       $GLOBALS['db']->query($sql);
      
      } 
    }
    }


  • I am not calling save. There's no infinite loop. I am logging in sugarcrm.log and found out that still it is running multiple times...
  • I'm having a similar issue using my logic hook in the leads modules at after_save event. I get running about 8 times after the event occurs. Any ideas why this is occuring and how do we get it run only once?

    This makes debugging even harder as having it run multiple times means you have to follow it though multiple times.
  • Hi Sone,
    Can you paste your code to check the issue.
  • Hi Harron,

    To prevent loop you have to let sugar bean know that bean is processed. 


    There are some flag with this object which prevent looping.

    for hook version - 1

    $this->processed = TRUE. 

    $this->logicHookDepth[$event] = 11  [ as max depth is this->max_logic_depth = 10 ]

    will prevent loop.


    You can see method - call_custom_logic of SugarBean class.





  • anyone could share a working solution?