1

I wrote a trigger for creating Child cases from a case, but it is going in infinite loop. as every child case that is created from parent Case calling the apex trigger.

trigger DemoTrigger on Case (after insert) {

    List<Case> childCases = new List<Case>();
    System.debug('Size' + Trigger.new);
        for(Case parent: Trigger.new){
            childCases.add( new Case(ParentId = parent.id, Subject = parent.Subject));
        }
    insert childCases;
}

I want to call a Apex Trigger only 3 time to create 3 child cases. how to put limit on number of calls? is there any way to do this?

2
  • 1
    Hi Ankur, welcome to SFSE. Please take a moment to visit the help center and read How to Ask. If you include the code you already have, you will get advice that is specifically tailored to your situation, which will make bot the question and the answer more valuable to the community.
    – Adrian Larson
    Feb 7, 2017 at 13:21
  • Note that you can properly format a code block by hitting CTRL + K or the {} button. I did so for you in this case.
    – Adrian Larson
    Feb 7, 2017 at 13:58

2 Answers 2

2

To avoid recursive calls generally, we use static variable. You can use a static integer as counter and stop when it reach the required value.

This is explained in this Salesforce link.

Avoid Recursive Trigger Calls

Description

Many developers face this issue because of a recursive trigger. For example, in an 'after update' trigger, a developer could be performing an update operation and this would lead to recursive call, and the error: "maximum trigger depth exceeded"

Resolution
In order to avoid the situation of recursive call, make sure your trigger is getting executed only one time. To do so, 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.

Class code

public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

Trigger code

trigger updateTrigger on anyObject(after update) {

    if(checkRecursive.runOnce())
    {
    //write your code here            
    }

}
3
  • 1
    while this may provide an answer, link-only answers become useless the moment the link goes down. It is a good idea to also show an example or copy the most relevant parts of the link you provided
    – Novarg
    Feb 7, 2017 at 14:03
  • This solution will only work when the 200 or fewer Cases are inserted. See my answer for a solution that overcomes that limitation.
    – SFDC Neuf
    Feb 7, 2017 at 18:45
  • @Novarg I have updated the answer as per your suggestion.
    – Saroj Bera
    Feb 8, 2017 at 10:44
2

A static variable to count executions will work correctly if you're working with 200 records or less in the initial trigger. If you have more than 200 records, the system will automatically batch them into groups of at most 200 until all records are processed (See here for more info). This means that when using a static variable, the first 200 records will get child cases created, while any additional records will not.

In order to replicate this functionality for more than 200 records at a time, a little more nuance is required. In the code below, I let the method itself create the cases 3 times, while using a static list of ids to prevent any child cases from triggering the creation of additional children.

static Set<Id> parentCaseIds;

public static void processCases(List<Case> triggerNew)

    Integer executions = 0;
    List<Case> casesToProcess = triggerNew;
    List<Case> childCases;

    while (executions < 3){

        childCases = new List<Case>();
        for(Case parent : casesToProcess){
            //Skip logic if entering trigger recursively
            if (parentCaseIds.contains(parent.Id)){
                break;
            }
            childCases.add( new Case(ParentId = parent.id, Subject = parent.Subject));
            parentCaseIds.add(parent.Id);
        }
        executions++;
        insert childCases;
        casesToProcess = childCases;
    }
}

This may seem a bit over-engineered, but if there is ever a possibility of more than 200 cases being created simultaneously, this code will still work.

3
  • 1
    You could remove the need for the static ids and use a static Boolean. Set it to true before the insert and false after. Then have the trigger check the Boolean to see if it should skip execution. Provides more finite control and reducing CPU cycles even if by a little
    – Eric
    Feb 7, 2017 at 19:27
  • Good point - since it's not an update, you don't have to worry about workflows repeating the logic.
    – SFDC Neuf
    Feb 7, 2017 at 19:42
  • Even if workflows were at play, it would not matter as the trigger would not execute. WFR would still operate on the inserted/updated records
    – Eric
    Feb 7, 2017 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .