Skip to main content

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;

    

    

    }
9 answers
  1. Jul 14, 2015, 5:35 AM
    Hi Anthony Zirilli 10,

    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

    public class ContactTriggerHandler

    {

    public static Boolean isFirstTime = true;

    }

    Trigger Code

    trigger ContactTriggers on Contact (after update)

    {

    Set<String> accIdSet = new Set<String>();

    if(ContactTriggerHandler.isFirstTime)

    {

    ContactTriggerHandler.isFirstTime = false;

    for(Contact conObj : Trigger.New)

    {

    if(conObj.name != 'Test')

    {

    accIdSet.add(conObj.accountId);

    }

    }

    // any code here

    }

    }

    I hope this will help you.

    In your case. Please create one handler class like below :-

    public class PrintCopyHandler

    {

    public static Boolean isFirstTime = true;

    }

    Then Update your Trigger like below :-

    trigger CreatePrintCopyLineItem on Print_Copy__c (before insert,after update)

    {

    if(PrintCopyHandler.isFirstTime)

    {

    PrintCopyHandler.isFirstTime = false;

    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 let us know if this will help you.

    Thanks

    Amit Chaudhary

    amit.salesforce21@gmail.com
0/9000