Skip to main content
Hi,  I am working on 2 triggers on 2 related objects. Basically, when some fields in object A are updated, the same fields in object B should be updated as object A is parent of object B. This should also work the other way around so when object B is updated, object A should also be updated. I have 2 triggers on each object and they both work when only one is active but not when both are active. I get the error SELF_REFERENCE_FROM_TRIGGER when both are active while updating either object. 

If anyone has any idea how to work around this I would really appreciate your help.

(P.S, I know that this can be done using process builder but I need to do this on the triggers for several reasons.).
1 risposta
  1. 26 giu 2020, 10:14
    SELF_REFERENCE_FROM_TRIGGER error from Apex indicates that you have attempted to update or delete a record that is already being updated by another trigger.

    Sometimes we need to update the record of the same object, which is initializing the trigger. This will cause a recursive trigger and throws this exception. For example, suppose you have a self lookup to contact named spouse. You have two contacts, A and B. when you select A as the spouse of B, you want to also update A as the spouse of B. In trigger for A when you update B, it will call recursive trigger.

    To control/prevent such condition, we need to add a static variable in another class, as this variable will share a single instance, as these transaction has occurred from a single event. This static variable allows the code to execute in the first run. but prevent any run thereafter.