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.
Parents
  • 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);
      
      } 
    }
    }


Reply
  • 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);
      
      } 
    }
    }


Children
No Data