AnsweredAssumed Answered

7.9.2 does getBeans in after_relationship_add logic_hook use cache?

Question asked by Francesca Shiekh on Sep 18, 2017
Latest reply on Oct 11, 2017 by Francesca Shiekh

I have a logic hook method on a custom OppProducts module related to Opportunites which executes an after_relationship_add, before_relationship_delete and before_save

While the before_save (for modifications) and before_relationship_delete (for deleted Products) work as designed, when I add a new product via the OppProducts subpanel on Opportunities, the after_relationship_add logic hook triggers but doesn't work as it did in 7.8.x.

 

In the logic hook, I retrieve the parent Opportunity and then loop through the products related to that opportunity. But the product I just added, which triggered the after_relationship_add, is not included in the products retrieved.

 

It worked in 7.8 though.

Has something changed in how after_relationship_add logic is handled in 7.9.2? (I've not tested other 7.9 releases)

Do I need to specify 'use_cache' => false in getBeans()? And if so, what is the correct syntax?

 

<?php
class OppProductsLogic {
   function add_product_values($bean, $event, $arguments){
      //for modified check if value was modified in the before save.
      //for added or deleted the relationship logic will take hold.
      if(($event == 'before_save' && !empty($bean->fetched_row) && $bean->fetched_row['value'] != $bean->value) || $event == 'after_relationship_add' || $event == 'before_relationship_delete' ){
         $GLOBALS['log']->fatal("add_product_values execute for " . $event);
         $relationship = 'oppp_opportunity_products_opportunities';
         $opps = array();
         $list = array();
         $products_of_interest = array();
         $bean->load_relationship($relationship);
         //for each opportunity related to this bean (there should be only one)
         foreach ($bean->$relationship->getBeans() as $opp){
            //look at all the products on that opportunity
            $opp->load_relationship($relationship);
            foreach ($opp->$relationship->getBeans() as $focus) {
               $GLOBALS['log']->fatal("relate product " . $focus->id);
               //if we are in the process of deleting this product skip this one
               //from products of interest and total amounts
               if($event=='before_relationship_delete' && $focus->id == $bean->id) continue;
               $sugarQuery = new SugarQuery();
               $sugarQuery->select(array('id','related_product_of_interest')); //each Opp Product has one or no related_product_of_interest
               $sugarQuery->from(BeanFactory::newBean('oppp_Opportunity_Product_Catalog'));
               $sugarQuery->where()->equals('name', $focus->product);
               $opp_prod = $sugarQuery->execute();
               if(!empty($opp_prod[0]['related_product_of_interest'])) $products_of_interest[]=$opp_prod[0]['related_product_of_interest']; //use the first one, should be unique
               //if it's a before save and the value has changed use the new value
               //from the bean we are about to save
               if($focus->id == $bean->id && $bean->fetched_row['value'] != $bean->value){
                  $value = $bean->value;
               }else{
                  $value = $focus->value;
               }
               $list[] = $value;
            }
            $sum = array_sum($list);
            if(isset($opp->reseller_commission_c) && $opp->reseller_commission_c != 0){
               if ($opp->reseller_percent_c == "1"){
                  $sum = $sum - ($sum * ($opp->reseller_commission_c  / 100));
               }else{
                  $sum = $sum - $opp->reseller_commission_c;
               }
            }
            $opp->amount = $sum;
            //also set reporting amount and reporting currency
            $opp->reporting_amount_c = $sum;
            if ($opp->currency_id == '-99'){
                $opp->reporting_currency_c = 'USD';
            }else{
                $objCurrency = BeanFactory::retrieveBean('Currencies', $opp->currency_id);
                $opp->reporting_currency_c = $objCurrency->iso4217;
            }
            $prods = encodeMultienumValue(array_filter(array_unique($products_of_interest)));
            $opp->product_of_interest_c = $prods;
            $opp->save();
         }
      }
   }
}
?>

Outcomes