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
SaaniaSaania 

Error ... SELF_REFERENCE_FROM_TRIGGER: between 2 triggers updating each other.

We have a custom object for Contact Roles. A field in Contacts (the gone field) updates the value of a field in 'Contact Roles', and a field in Contact Roles when updated (not one field but a combination of fields) has to set the value of a field in Contact. I know its a loop, but the requirements are such that I cant think of a work around. The trigger in 'Contact Roles' has to be an 'after', while the one in Contact is 'before'

 

Is there a way to specify the order in which these triggers are executed, or stop execution of one trigger till the other completes its execution?

 

Thanks,

Saania

IanRIanR

Hi,

 

I've been thinking about this... Here's somthing you can try:

 

Create a public class with a public static method ShouldTriggerRun() that returns a boolean, and then a static method RunTrigger that takes a collection of Sobjects that does the work (probably one for each trigger, but you may be able to merge the logic into one method - I haven't fully tested this theory out)

 

 

Something like:

 

 

public class TriggerHelper { private static boolean runFlag = true; public static boolean ShouldTriggerRun() { return runFlag; } public static void UpdateContacts(Contact[] contacts) { // Do not perform this work if we have turned the flag off if (!runFlag) return; // Set the run flag off for subsequent triggers fired by the updates we will do... runFlag = false; // Do Work on the Contacts and Contact Roles here... } public static void UpdateContactRoles(ContactRole__c contactRoles) { // Do not perform this work if we have turned the flag off if (!runFlag) return; // Set the run flag off for subsequent triggers fired by the updates we will do... runFlag = false; // Do Work on the Contacts and Contact Roles here... }}

 

 Then in the Triggers...

 

 

 

Trigger UpdateContacts on Contact (before update) { TriggerHelper.UpdateContacts(Trigger.new);}Trigger UpdateContactRoles on ContactRole__c (before update) { TriggerHelper.UpdateContactsRoles(Trigger.new);}

 

 

 

 

 Hope this helps,

 

Ian