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?
Best Answer
It looks like your method
Automations.UpdateOpportunityLineItems
is causing the loop so I would recommend a couple of fixes:You probably need to set
already=true
earlier, immediately after theif (already==false)
statement. By delaying this assignment to the end you aren't preventing the loop.Do not allow the called methods to execute DML; instead, provide them with a global
static map<Id, sObject>
variable for each sObject type. These methods would place the updated records in the global object, which would then be updated at the end.