function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JohnRyderJohnRyder 

Infinite trigger problem

Hey,

 

I have object X AND Y.When a record is created in X,the trigger creates same record in Y also update,delete,undelete and viceversa.This cause infinite trigger for me.

 

I have seen a @future method in web and understood how its handle the problem but @future method works with static methods and " Methods with the future annotation cannot take sObjects or objects as arguments.

 

I pass trigger.new as argument and take it as a list to insert as a record in my method.In static @ future examples,I have seen that they pass trigger.new's id's as set<id> and do their job. @Future method accepts the sets as parameter.I don' t know the way of inserting a new record by passing trigger.new as id.I know that I have to pass it as an object.So I couldn't find the way of preventing infinite trigger.Can you help me with this?

 

Regards.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

The usual means of accomplishing this is through a static variable:

 

public class TriggerUtils {
    public static Boolean recursive = false;
}

trigger X on X (events) {
    if(TriggerUtils.recursive) {
        return;
    }
    TriggerUtils.recursive = true;
    // Perform updates on Y
    TriggerUtils.recursive = false;
}

trigger Y on Y (events) {
    if(TriggerUtils.recursive) {
        return;
    }
    TriggerUtils.recursive = true;
    // Perform updates on X
    TriggerUtils.recursive = false;
}

 

All Answers

sfdcfoxsfdcfox

The usual means of accomplishing this is through a static variable:

 

public class TriggerUtils {
    public static Boolean recursive = false;
}

trigger X on X (events) {
    if(TriggerUtils.recursive) {
        return;
    }
    TriggerUtils.recursive = true;
    // Perform updates on Y
    TriggerUtils.recursive = false;
}

trigger Y on Y (events) {
    if(TriggerUtils.recursive) {
        return;
    }
    TriggerUtils.recursive = true;
    // Perform updates on X
    TriggerUtils.recursive = false;
}

 

This was selected as the best answer
JohnRyderJohnRyder
Thanks a lot.I handled the issue by applying similar logic as in your piece of code.