[SalesForce] SELF_REFERENCE_FROM_TRIGGER error when updating child record

Im confused becauyse this trigger code is working in the sandbox and was working in production yesterday but today im getting

Error:Apex trigger sendDAP1Invites caused an unexpected exception,
contact your administrator: sendDAP1Invites: execution of BeforeUpdate
caused by: System.DmlException: Upsert failed. First exception on row
0 with id a04U0000009HitMIAS; first error:
SELF_REFERENCE_FROM_TRIGGER, Object (id = a05U0000004JwIT) is
currently in trigger sendDAP1Invites, therefore it cannot recursively
update itself: []: Trigger.sendDAP1Invites: line 18, column 1

The trigger code is very simple all its doing is checking a checkbox on a child object when a parent object is updated?

trigger sendDAP1Invites on DAP_Session__c (before update) {


  List<DAP_Session_Assignment__c> rows = new List<DAP_Session_Assignment__c>();
  for (DAP_Session__c session : Trigger.new) {
      if (session.Invite_Status__c == 'live') {
          // check old session status
          DAP_Session__c oldSession = Trigger.oldMap.get(session.ID);
          if (session.Invite_Status__c != oldSession.Invite_Status__c) {

                 for (DAP_Session_Assignment__c score : [Select Id, Invited__c from DAP_Session_Assignment__c where DAP_Session__c = :session.id ]) {
                    //update all the scores to invited!
                     score.Invited__c = true;
                     rows.add(score);
                 }

          }

      }
  }
  upsert rows;

}

Best Answer

You are trying to using a BEFORE trigger, and in your trigger, you are trying to do a DML call on records that are in the trigger. In BEFORE triggers, any manipulation you do to the data happens before the records undergo their DML call, so you don't need to perform a DML call on the trigger records -- it's already gonna happen. However, you would need to do a DML call on AFTER triggers. They happen after the trigger, so anything you do AFTER the trigger needs to be "saved" via a DML call.

So, for your case, just remove the upsert rows; and it should work.

To note, I usually use BEFORE triggers if I need alter data that is being fed into the trigger while I use AFTER triggers if I need to alter data that is related to the data being fed into the trigger.

BEFORE Example:

trigger MyCustomObj_beforeTrigger on MyCustomObj__c (before update){

    for(MyCustomObj__c m:trigger.new){
       //do something to the trigger data
    }
}

AFTER Example:

trigger MyCustomObj_afterTrigger on MyCustomObj__c (afterupdate){
    list<myChildObj__c> newChildrenList = new list<myChildObj__c>();

    for(MyCustomObj__c m:trigger.new){
       newChildrenList.add(new myChildObj__c());
    }

    insert newChildrenList;
}

Also, one bit of advice on triggers: write all the code you need for your trigger in its own class, and then just call the class in the trigger (and make sure you use all the trigger goodies when you can -- trigger.new, trigger.old, trigger.newMap, trigger.oldMap, trigger.isInsert, trigger.isUpdate, trigger.isDelete ).

It's good to do this to keep your triggers as short and simple as possible. Then, I also recommend that you have only 1 trigger per custom object (so you can easily tell what exactly is being triggered in any situation). And if you need to do separate things in your trigger, just call the classes as needed.

Example:

Class code: public class trigger_CreateNewChildren(){

    public static void CreateNewChildren(map<id,MyCustomObj__c>newMap){
        list<MyChildrenObj__c> newChildren = new list<MyChildrenObj__c>();

        for(MyCustomObj__c m:newMap.values()){
            newChildren.add(new MyChilerenObj__c());
        }

        insert newChildren;

    }//END CreateNewChildren

    public static void fieldChangeCheck(map<id,MyCustomObj__c> newMap,map<id,MyCustomObj__c>oldMap){

        for(id m:newMap.keyset()){
            if(newMap.get(m).field_1__c!=oldMap.get(m).field_1__c)
                //do something
        }

    }//ENDfieldChangeCheck

Trigger code:

Trigger MyCustomObj on MyCustomObj__c(AFTER INSERT,BEFORE UPDATE){

    if(trigger.isInsert && trigger.isAfter){
        trigger_CreateNewChildren.CreateNewChildren(trigger.newMap);
    }

    if(trigger.isUpdate && trigger.isBefore){
        trigger_CreateNewChildren.fieldChangeCheck(trigger.newMap,trigger.oldMap);
    }
} 
Related Topic