In an 'after save' logic hook, there is a process that checks for any records that match the ID of the Opportunity, and the Special Key of the Revenue Line Item to match the same values in the Quoted Line Item (Special Key - QLI_key_c - is the ID of the Quoted Line Item). If none exist, then the code will initiate the creation of a new Revenue Line Item. What is happening is the code runs multiple times, then is halted by Sugar's own processes.
I tried using two different methods to prevent multiple runs of the code after the initial action, but none were successful. And 'save()' must be used to store the data in this 'after save' logic hook.
Is there a way using the code to prevent the loops that create extra copies?
[Must include that a separate JS file has deleted previous Revenue Line Items, and then this logic hook should add new ones from a new set of Quoted Line Items.]
<?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class QuotedLineConvert { static $already_ran = false; function saveQuotedLineItems($bean, $event, $arguments) { if(self::$already_ran == true) return; self::$already_ran = true; // if (!isset($bean->ignore_update_c) || $bean->ignore_update_c === false) { // get the Quotes related to the Opportunities $oppquo = 'opportunities_qm_quotes_module_1'; if($bean->load_relationship($oppquo)){ $quotes = $bean->{$oppquo}->getBeans(); foreach($quotes as $quo){ // get the Quoted Line Items ($qli) in each Quote $quoqli = 'qm_quotes_module_qli_quoted_line_items_1'; if($quo->load_relationship($quoqli)){ $qlines = $quo->{$quoqli}->getBeans(); foreach($qlines as $qli){ // establish Revenue Line Item ($rli) for each $qli and add values to the $rli $newrli = $this->getRLILinks($bean, $qli->id); if(!$newrli){ continue; } $newrli->sales_stage = 'Proposal/Price Quote'; $newrli->qli_key_id_c = $qli->id; $newrli->name = $qli->name; $newrli->item_no_c = $qli->inventory_id_c; $newrli->discount_price = $qli->discounted_unit_price_c; $newrli->requested_on_c = $qli->requested_onn_c; $newrli->quantity = $qli->order_qty_c; $newrli->likely_case = ($qli->discounted_unit_price_c * $qli->order_qty_c); $newrli->assigned_user_id = $qli->assigned_user_id; $newrli->opportunity_id = $bean->id; $bean->assigned_user_id = $qli->assigned_user_id; $rlidate = $quo->quote_expiration_date_c; $rlidate = date('Y-m-d',strtotime('+30 days',strtotime($rlidate))); $newrli->date_closed = $rlidate; // add relationship between $rli and $qli $revquo = 'revenuelineitems_qli_quoted_line_items_1'; $newrli->load_relationship($revquo); $newrli->{$revquo}->add($qli->id); $newrli->save(); } } } } } } // find any links the $rli to the $qli using 'qli_key_c' and create the new $rli if none exist. public function getRLILinks($bean, $qli_id) { $sugarQuery = new SugarQuery(); $sugarQuery->from(BeanFactory::getBean('RevenueLineItems')); $sugarQuery->select(['id']); $sugarQuery->where() ->equals('qli_key_id_c', $qli_id) ->equals('opportunity_id', $bean->id); $result = $sugarQuery->getOne(); if($result){ return false; }else{ $newrli = BeanFactory::newBean('RevenueLineItems'); return $newrli; } } } ?>