You need to sign in to do that
Don't have an account?
deplai
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; } } } }
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
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.
How would I go about doing that?
Check this link:
http://boards.developerforce.com/t5/Apex-Code-Development/Trigger-is-fired-twice-due-to-the-workflow-rule-field-update/m-p/123488#M14861
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;
}
}
Thanks Dream_weaver. That did the trick!