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
deplaideplai 

Error on Afrter Update trigger: Maximum trigger depth exceeded

I created a custom object called Feature.  For each Feature, I've added multiple Products.  On an Opportunity, I made a lookup field to the Feature object.

When I add a Feature, the trigger should add all the Products associated to that Feature as product line items into the Opportunity. 

The code works for After Insert, but will not work for After Update.  I get the error if I try to do an update.  Is there something I can do to my code to get this trigger to work after an update?

trigger OpportunityTrigger2 on Opportunity (after insert, after update) {
// List containing each Opportunity being processed
list<Opportunity> OppId = new list<Opportunity>();
// Go through each opportunity and add it to the OppId list 
for(Opportunity l:trigger.new) {
//Now add them to the list
OppId.add(l); 
}

// Now for each Opportunity in the list
for (Opportunity l:OppId) {
// Get the ID for the Feature of that Opportunity
Id FeatureId = l.Feature__c;
// Now I have the Id of the feature, create a list of the products for that feature
list<Product2> theprod = [SELECT Id, Name FROM Product2 WHERE Features__c = :FeatureId ];  

// Now lets go through each of these products
for (Product2 p:theprod) {
// Now lets get the details we need from the price book entry to insert our Opportunity line item
list<PricebookEntry> TheEntry = [SELECT Id, UnitPrice, Name, Product2Id FROM PricebookEntry where Product2id = :p.id and Pricebook2Id = '01sA00000001tiRIAQ'];
// For each of these pricebook entries
for (PricebookEntry e:TheEntry) {
// Create a new opportunity line item
OpportunityLineItem newoppprod = new OpportunityLineItem(
    // This tells the line item which opportunity to link it to
    opportunityId = l.id,
    // Add the quantity, which is usually always 1
    Quantity = 1,
    // Add the Price
    UnitPrice = e.UnitPrice,
    // Add the pricebook entry ID to tell it what the line item links to in the pricebook entry
    PricebookEntryId = e.id
);
// and now insert that line item
insert newoppprod;
}

}
}
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Dream_weaverDream_weaver

I have described the solution with example..If satisfied mark this ans as solution..It will help others..

 

maximum trigger depth error
trigger createRecurring on Service_Order__c(after update, after insert)
{  
    
    List<Service_Order__c> soInsert = new List<Service_Order__c>();
    for (Service_Order__c hr : [Select id,Recurring__c,Building__c, RecordTypeId from Service_Order__c where id in: Trigger.new])
    {
            if(hr.Recurring__c == True){
            Service_Order__c newSO = hr.clone(true);
            soInsert.add(newSO);      
            }
    }
    update soInsert;
}------------------------------->this was the initial code
----------------------------------------------------------------------------
now,Since you're updating the records that originally fired the trigger, it goes into a recurisve loop, trying to invoke itself.

So you need to use a static variable to break the recursion.

 

Create a class or use an existing class, where you declare a static variable


public with sharing class TriggerUtil{

public static boolean serviceOrderExecuting = false;

}

Trigger:

trigger createRecurring on Service_Order__c(after update, after insert)
{  
//check that the trigger is not already executing   

if (!TriggerUtil.serviceOrderExecuting){

TriggerUtil.serviceOrderExecuting = true;
    List<Service_Order__c> soInsert = new List<Service_Order__c>();
    for (Service_Order__c hr : [Select id,Recurring__c,Building__c, RecordTypeId from Service_Order__c where id in: Trigger.new])
    {
            if(hr.Recurring__c == True){
            Service_Order__c newSO = hr.clone(true);
            soInsert.add(newSO);      
            }
    }
    update soInsert;

}
}

All Answers

JBabuJBabu

Hi,

 

I think the problem is due to the action happening multiple times.

You need to block the action from happening it several times.

 

Thanks,

Babu.

deplaideplai

How would I go about doing that?

Dream_weaverDream_weaver

I have described the solution with example..If satisfied mark this ans as solution..It will help others..

 

maximum trigger depth error
trigger createRecurring on Service_Order__c(after update, after insert)
{  
    
    List<Service_Order__c> soInsert = new List<Service_Order__c>();
    for (Service_Order__c hr : [Select id,Recurring__c,Building__c, RecordTypeId from Service_Order__c where id in: Trigger.new])
    {
            if(hr.Recurring__c == True){
            Service_Order__c newSO = hr.clone(true);
            soInsert.add(newSO);      
            }
    }
    update soInsert;
}------------------------------->this was the initial code
----------------------------------------------------------------------------
now,Since you're updating the records that originally fired the trigger, it goes into a recurisve loop, trying to invoke itself.

So you need to use a static variable to break the recursion.

 

Create a class or use an existing class, where you declare a static variable


public with sharing class TriggerUtil{

public static boolean serviceOrderExecuting = false;

}

Trigger:

trigger createRecurring on Service_Order__c(after update, after insert)
{  
//check that the trigger is not already executing   

if (!TriggerUtil.serviceOrderExecuting){

TriggerUtil.serviceOrderExecuting = true;
    List<Service_Order__c> soInsert = new List<Service_Order__c>();
    for (Service_Order__c hr : [Select id,Recurring__c,Building__c, RecordTypeId from Service_Order__c where id in: Trigger.new])
    {
            if(hr.Recurring__c == True){
            Service_Order__c newSO = hr.clone(true);
            soInsert.add(newSO);      
            }
    }
    update soInsert;

}
}

This was selected as the best answer
deplaideplai

Thanks Dream_weaver.  That did the trick!