Skip to main content

Hello Everyone,

Actually I have doubt in recursion trigger. As recursion basically means one trigger calling same trigger or another trigger and that trigger is calling 1st trigger, this can be resolved by using boolean variable, but my doubt is if i want to run that logic for 2 times how should i implement it. As with variable the method runs only once.

15 件の回答
  1. 2023年7月19日 10:13

    I can help you with that. To run a recursive trigger logic for 2 times, you can use the following steps:

    1. Create a boolean variable called isFirstTime and initialize it to true.
    2. In your trigger, add the following code:

    if (isFirstTime) {

    // Do something

    isFirstTime = false;

    // Trigger the same trigger again

    trigger.fire();

    }

    This code will first check if the value of isFirstTime is true. If it is, then the code will do something and then set the value of isFirstTime to false. After that, the code will trigger the same trigger again. This will cause the trigger to be executed twice.

    Here is an example of a recursive trigger that can be used to run a logic for 2 times:

    trigger MyTrigger on Account (after insert) {

    // Initialize the boolean variable

    boolean isFirstTime = true;

    // Do something

    insert new Account(Name = 'Account 1');

    // If it is the first time, trigger the trigger again

    if (isFirstTime) {

    isFirstTime = false;

    trigger MyTrigger();

    }

    }

    This trigger will first insert a new Account record with the name Account 1. After that, the trigger will check if the value of isFirstTime is true. If it is, then the trigger will set the value of isFirstTime to false and then trigger itself again. This will cause the trigger to be executed twice. The first time, the trigger will insert the Account record. The second time, the trigger will do nothing, as isFirstTime will be false. 

0/9000