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
BrokenBirdBrokenBird 

Cascading trigger

In a custom object I have a checkbox that I want to be unique for a group of records based on a relation. In example for all the records related to the same account, I want only one to have this field checked. 

 

To do so I implemented a trigger that would uncheck all the other records after new one is checked and also update a field in the account record; when the record is unchecked, I clear the field in the account.

 

The problem I have is that this provoke triggers on the modified records as well, now how do I know if the trigger is the original one or a cascaded one?

Best Answer chosen by Admin (Salesforce Developers) 
aalbertaalbert

Use a public static variable in a common Class to differentiate between Triggers. One trigger sets the variable , and the other can check its value.

 

For example:

 

 

public class utilClass { public static boolean triggerVariable = true;

}

 

 

 

trigger AccountTest on Account (before insert, before update) { if(utilClass.triggerVariable == true){ //Do some logic //Set the variable to true so the other trigger's can check its value. utilClass.triggerVariable = false; } }

 

 

 

trigger ContactTrigger on Contact (before insert, before update) { if(utilClass.triggerVariable = false){ //Do some logic //Set the variable to true so the other trigger's can check its value. utilClass.triggerVariable = true; } }

 

 

All Answers

BrokenBirdBrokenBird
To be clearer, what I do is to rollup a child value to a parent that is not master-detail because I already have one level. Otherwise this would have been quite simple.
aalbertaalbert

Use a public static variable in a common Class to differentiate between Triggers. One trigger sets the variable , and the other can check its value.

 

For example:

 

 

public class utilClass { public static boolean triggerVariable = true;

}

 

 

 

trigger AccountTest on Account (before insert, before update) { if(utilClass.triggerVariable == true){ //Do some logic //Set the variable to true so the other trigger's can check its value. utilClass.triggerVariable = false; } }

 

 

 

trigger ContactTrigger on Contact (before insert, before update) { if(utilClass.triggerVariable = false){ //Do some logic //Set the variable to true so the other trigger's can check its value. utilClass.triggerVariable = true; } }

 

 

This was selected as the best answer
BrokenBirdBrokenBird
My project does not use classes, I will research how to create this, thanks.
BrokenBirdBrokenBird

Works thanks, just had to add an extra local variable to unset the static after the update if I was the originator.