2

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;

}
  • 1
    You are using SOQL (and dml) in a loop: wiki.developerforce.com/page/… Has someone recently added workflow or a trigger on the DAP_Session_Assignment__c object? – Daniel Blackhall Aug 22 '13 at 22:39
  • hmm right, i fixed the DML loop issue (edited above as well) but still getting error. I did add a rollup var to count to the parent object? is that causing this? how would you suggest getting around that? – Keith Mancuso Aug 22 '13 at 22:49
  • I asked a similar question some time ago: salesforce.stackexchange.com/questions/109/… – Daniel Blackhall Aug 22 '13 at 23:29
  • Does the parent object have roll up summary fields? Also, this should really be an after trigger since you're not modifying your DAP_Session__c records in the trigger. – Adrian Larson Aug 23 '13 at 0:48
4

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);
    }
} 
  • wait but an upsert on a child object wouldnt happen automatically would it? I know it automatically pushes the update to the object being triggered, but i dont think its doing it for child objects? – Keith Mancuso Aug 23 '13 at 15:48
  • and for the record the reason im using a before update is because im also updating the parent object I just left it out of my example for simplicity – Keith Mancuso Aug 23 '13 at 15:49
  • ah, i didn't catch that at first. i see that you're upserting records of a different object. My guess is the SELF-REFERENCE problem stems from trying to do a DML on already triggered records. Do the records that you're doing a DML on have a trigger that re-calls the already triggered records -- like a loop where the records keep re-triggering themselves? – Scott Pelak Aug 23 '13 at 15:57
  • PS I added the example at the end not for you but for whoever stumbles upon your post – Scott Pelak Aug 23 '13 at 16:02
  • Hi All I have same problem.Can you please click this link salesforce.stackexchange.com/questions/42537/… See the log I a have got same issue. – Ramesh S Jul 11 '14 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.