function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
EnryEnry 

Recursive trigger

This is my trygger on Opportunity object.

 

trigger AutoProductupdate on Opportunity ( before update,after update) {
  static boolean already=false;
  if (already==false){
    for(Opportunity newOppt : Trigger.new){       
      if  (trigger.isAfter){
        if (newOppt.Pricing_Tears__c.trim()=='Gigabyte'){                                   
          SYSTEM.DEBUG('update');

          Automations.UpdateOpportunityLineItems(newOppt);
          //Action of the called function: 
          //DELETE OPPORTUNITY LINE ITEM SCHEDULES FOR ALL THE PRODUCTS
          // UPDATE OPPORTUNITY LINE ITEM
          Automations.SheduleOpportunityLineItem(newOppt);
          //Action of the called function: 
          // CREATE OPPORTUNITY LINE ITEM SCHEDULES FOR ALL THE PRODUCTS
        }
      }  
    }        

    already=true; 
   }                   
}

 

I am working with opportunity product and opportunity product schedules. In the debug log i see that the trigger is working recursively (It 's printed 15 times the string 'update' with the system.debug).

The error message:

 

execution of AfterUpdate caused by: System.DmlException: Delete failed. First exception on row 0 with id 00oL0000000acVqIAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AutoProductupdate: maximum trigger depth exceeded Opportunity trigger event AfterUpdate for [006L0000003EUVZ] Opportunity trigger event AfterUpdate for [006L0000003EUVZ] Opportunity trigger event AfterUpdate for [006L0000003EUVZ] Opportunity trigger event AfterUpdate for [006L0000003EUVZ] Opportunity trigger event AfterUpdate for [006L0000003EUVZ] Opportunity trigger event AfterUpdate for [006L0000003EUVZ] Opportunity trigger event AfterUpdate for [006L0000003EUVZ] Opportunity trigger event AfterUpdate for [006L0000003EUVZ] Opportunity trigger event AfterUpdate for [006L0000003EUVZ] Opportunity trigger event AfterUpdate for [006L0000003EUVZ] Opportunity trigger event AfterUpdate for [006L0000003EUVZ] Opportunity trigger event AfterUpdate for [006L0000003EUVZ] Opportunity trigger event AfterUpdate for [006L0000003EUVZ] Opportunity trigger event AfterUpdate for [006L0000003EUVZ] Opportunity trigger event AfterUpdate for [006L0000003EUVZ] Opportunity trigger event AfterUpdate for [006L0000003EUVZ]: []: Class.Automations.UpdateOpportunityLineItems: line 70, column 1

 

Which is the problem?

How can avoid this?

EnryEnry

Solution: Use a static variable

in an Apex class

(you mustn't set the static variable in the trigger, create a class) to avoid an infinite loop.

http://www.salesforce.com/docs/developer/cookbook/Content/apex_controlling_recursive_triggers.htm

{tushar-sharma}{tushar-sharma}
Hi,
We have multiple ways to avoid recursive triggers. They all depend on your use case, While one solution may work but will not work for others. Check the below links, It has multiple ways to avoid recursive trigger: https://newstechnologystuff.com/2020/05/28/avoid-recursive-trigger-in-salesforce/

Many Developers face recursive trigger or recursive update trigger. For example in 'after update' trigger, the Developer is performing update operation, and this leads to the recursive call. Or if we have WF field update sometimes they also contribute to recursive triggers.