You need to sign in to do that
Don't have an account?
Create multiple records without recursively firing trigger
Hi there,
I'm a little stuck on how to create a number of new child records on a parent object. I've been able to do it below a little half haphazardly by having the trigger fire multiple times until my two fields, “copy count” and a roll up field “Total Number of Line Items” are equal. Any insight on how to approach this to create the multiple line item records when Copy Count is less than the Total Number of Line Items so that copy count is equal to total number of line items. Thank you in advance.
Anthony
Current trigger below (When copy count increases by 15, "maximum trigger depth excedded" error is tripped):
trigger CreatePrintCopyLineItem on Print_Copy__c (before insert,after update) {
List <Print_Copy_Line_Item__c> LineItemToInsert = new List <Print_Copy_Line_Item__c> ();
for (Print_Copy__c PrintCopy : Trigger.new) {
if (PrintCopy.Copy_Count__c>PrintCopy.Total_Number_of_Line_Items__c) {
Print_Copy_Line_Item__c LineItemToAdd = new Print_Copy_Line_Item__c ();
LineItemToAdd.Print_Copy__c=PrintCopy.Id;
LineItemToInsert.add(LineItemToAdd);
}
}
insert LineItemToInsert;
}
Please check below blog how to stop recursive trigger in salesforce
http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html
Solution :-
you can create a class with a static Boolean variable with default value true. In the trigger, before executing your code keep a check that the variable is true or not. Once you check make the variable false.
Apex Class with Static Variable
Trigger Code
I hope this will help you.
In your case. Please create one handler class like below :-
Then Update your Trigger like below :-
Please let us know if this will help you.
Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
All Answers
You are getting this error because your trigger recursively calling himself. So to overcome with that you have to declare a static variable (Boolean type) in another class and make certain criteria based on that static variable inside your trigger. Please go through this link for more details:
http://developer.force.com/cookbook/recipe/controlling-recursive-triggers
Please let me know if it resovles your problem.
Best Regards,
-Vivek
vivek.deshmane@gmail.com
Your trigger fall under recursion as your roll up summery field will update parent record and after that again your trigger will fire after update .
So you need to stop recursion by using below code .
Let us know if it help !!
Thanks
Manoj
public Class checkRecursive{ private static boolean run = true; public static boolean runOnce(){ if(run){ run=false; return true; }else{ return run; } } }
Please check below blog how to stop recursive trigger in salesforce
http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html
Solution :-
you can create a class with a static Boolean variable with default value true. In the trigger, before executing your code keep a check that the variable is true or not. Once you check make the variable false.
Apex Class with Static Variable
Trigger Code
I hope this will help you.
In your case. Please create one handler class like below :-
Then Update your Trigger like below :-
Please let us know if this will help you.
Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
Try the below code.
This might work correctly as you expected.
Thanks
Use the below code.
Hope this works as you expected.
Thanks