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
AmberTaylorAmberTaylor 

SELF_REFERENCE_FROM_TRIGGER, Object (id = a1G0E000001T6wg) is currently in trigger OC_EquipementUpdateWithReferentiel, therefore it cannot recursively update itself: []

Hello there,

I have an issue with my trigger, here is the error message that i get :  : System.DmlException: Update failed. First exception on row 0 with id a1G0E000001T6wgUAC; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = a1G0E000001T6wg) is currently in trigger OC_EquipementUpdateWithReferentiel, therefore it cannot recursively update itself: [] Class.OC_EquipementTRIGGER.updateParent: line 181, column 1 Trigger.OC_EquipementUpdateWithReferentiel: line 23, column 1: []: Class.OC_EquipementTRIGGER.UpdateEquipWithReferentiel: line 73, column 1

Here is my trigger Class :
public class OC_EquipementTRIGGER {
    public static Boolean runTrigger = true;
    Public static Boolean firstcall=true;
    
    // Trigger 1 active when record type = 'Equipement referentiel'
    public static void UpdateEquipWithReferentiel(List<OC_Equipement__c> equipRefCreated) {
        list<OC_Equipement__c> updateParent = new list<OC_Equipement__c>();
        
        List<String> IMEICodeRef = new List<String>();
        List<String> SILCodeRef = new List<String>();
        List<String> serialNumberRef = new List<String>();
        
        String equipCollab='0120E000001bH9x';
        String equipReferentiel ='0120E000001bHA2';
        
        List <OC_Equipement__c> updateChildren= new List<OC_Equipement__c>();   
        
        Map<String, String> equipRefSIL = new Map<String, String>();
        Map<String, String> equipRefIMEI = new Map<String, String>();
        Map<String, String> equipRefSerial = new Map<String, String>();
        
        //Store all identifiers for newly created / updated records type référentiel in variables
        for (OC_Equipement__c e : equipRefCreated) {
            if (e.RecordTypeId == equipReferentiel) {
                if(e.NumeroIMEIiPhone__c != null) {
                    IMEICodeRef.add(e.NumeroIMEIiPhone__c);
                } else if(e.NumeroSIL__c != null) {
                    SILCodeRef.add(e.NumeroSIL__c);
                } else if(e.NumeroSerie__c != null) {
                    serialNumberRef.add(e.NumeroSerie__c);
                } 
            }
        }
        
        //  List of equipements collaborateur corresponding to newly created referentiel equipements
        List <OC_Equipement__c> equipList = [Select ID,Name, NumeroTelephone__c, NumeroSIL__c, EquipementReferentiel__c, TypeEquipement__c,  NumeroSerie__c, NumeroIMEIiPhone__c, DateCreation__c,DeclarationEquipement__r.Nom__c , Statut__c 
                                             FROM OC_Equipement__c
                                             where ((NumeroSIL__c in :SILCodeRef) OR (NumeroSerie__c in :serialNumberRef) OR (NumeroIMEIiPhone__c in :IMEICodeRef)) AND RecordTypeId =:equipCollab];
        
        for (OC_Equipement__c eq:equipRefCreated){
            if (equipList.size() > 0 ){
                if(eq.NumeroIMEIiPhone__c != null) {
                    equipRefIMEI.put(eq.NumeroIMEIiPhone__c, eq.ID);
                } else if(eq.NumeroSIL__c != null ) {
                    equipRefSIL.put(eq.NumeroSIL__c, eq.ID);
                } else if(eq.NumeroSerie__c != null) {
                    equipRefSerial.put(eq.NumeroSerie__c, eq.ID);
                } 
            }
        } 
        
        System.debug('==equipRefSIL==' + equipRefSIL);
        System.debug('==equipRefIMEI==' + equipRefIMEI);
        System.debug('==equipRefSerial==' + equipRefSerial);
        
        for(OC_Equipement__c equip: equipList) {
            if(equipRefIMEI.containsKey(equip.NumeroIMEIiPhone__c)) {
                equip.EquipementReferentiel__c = equipRefIMEI.get(equip.NumeroIMEIiPhone__c);
                equip.statut__c = 'Présent et Utilisé';
                updateChildren.add(equip);
            } else if(equipRefSIL.containsKey(equip.NumeroSIL__c)) {
                equip.EquipementReferentiel__c = equipRefSIL.get(equip.NumeroSIL__c);
                equip.statut__c = 'Présent et Utilisé';
                updateChildren.add(equip);
            } else if(equipRefSerial.containsKey(equip.NumeroSerie__c)) {
                equip.EquipementReferentiel__c = equipRefSerial.get(equip.NumeroSerie__c);
                equip.statut__c = 'Présent et Utilisé';
                updateChildren.add(equip);
            }
            
            System.debug(equip.EquipementReferentiel__c + ' = equip.EquipementReferentiel__c value');
        }
        update updateChildren;
        System.debug(updateChildren + ' = updateChildren list value');
        
    }
    
    
    //trigger2  active when record type = 'Equipement Collaborateur'
    public static void UpdateEquip(List<OC_Equipement__c> equipCollabCreated) {
        List<String> IMEICode = new List<String>();
        List<String> SILCode = new List<String>();
        List<String> serialNumber = new List<String>();
        
        String equipCollab='0120E000001bH9x';
        String equipReferentiel ='0120E000001bHA2';
        
        List <OC_Equipement__c> equipToUpdate = new List<OC_Equipement__c>();  
        
        Map<String, String> equipCollabSIL = new Map<String, String>();
        Map<String, String> equipCollabIMEI = new Map<String, String>();
        Map<String, String> equipCollabSerial = new Map<String, String>();
        
        
        //Store all identifiers for newly created / updated records type equip collaborateur in variables
        for (OC_Equipement__c eq : equipCollabCreated) {
            if (eq.RecordTypeId == equipCollab) {
                if(eq.NumeroIMEIiPhone__c != null) {
                    IMEICode.add(eq.NumeroIMEIiPhone__c);
                } else if(eq.NumeroSIL__c != null ) {
                    SILCode.add(eq.NumeroSIL__c);
                } else if(eq.NumeroSerie__c != null) {
                    serialNumber.add(eq.NumeroSerie__c);
                } 
            }
        }
        
        //  List of equipements référentiel corresponding to newly created team equipements
        List <OC_Equipement__c> equipRefList = [Select ID,Name, NumeroTelephone__c, NumeroSIL__c, EquipementReferentiel__c, TypeEquipement__c,  NumeroSerie__c, NumeroIMEIiPhone__c, DateCreation__c,DeclarationEquipement__r.Nom__c , Statut__c 
                                                FROM OC_Equipement__c
                                                where ((NumeroSIL__c in :SILCode) OR (NumeroSerie__c in :serialNumber) OR (NumeroIMEIiPhone__c in :IMEICode)) AND RecordTypeId =:equipReferentiel];
        
        
        for (OC_Equipement__c eqList:equipRefList){
            if (equipRefList.size() > 0 ){
                if(eqList.NumeroIMEIiPhone__c != null) {
                    equipCollabIMEI.put(eqList.NumeroIMEIiPhone__c, eqList.ID);
                } else if(eqList.NumeroSIL__c != null ) {
                    equipCollabSIL.put(eqList.NumeroSIL__c, eqList.ID);
                } else if(eqList.NumeroSerie__c != null) {
                    equipCollabSerial.put(eqList.NumeroSerie__c, eqList.ID);
                } 
            }
        }
        
        System.debug('==equipCollabSIL==' + equipCollabSIL);
        System.debug('==equipCollabIMEI==' + equipCollabIMEI);
        System.debug('==equipCollabSerial==' + equipCollabSerial);
        
        for(OC_Equipement__c e: equipCollabCreated) {
            if(e.RecordTypeId == equipCollab)  {
                if(equipCollabIMEI.containsKey(e.NumeroIMEIiPhone__c)) {
                    e.EquipementReferentiel__c = equipCollabIMEI.get(e.NumeroIMEIiPhone__c);
                    e.Key__c = e.NumeroIMEIiPhone__c;
                } else if(equipCollabSIL.containsKey(e.NumeroSIL__c)) {
                    e.EquipementReferentiel__c = equipCollabSIL.get(e.NumeroSIL__c);
                    e.Key__c = e.NumeroSIL__c;
                } else if(equipCollabSerial.containsKey(e.NumeroSerie__c)) {
                    e.EquipementReferentiel__c = equipCollabSerial.get(e.NumeroSerie__c);
                    e.Key__c = e.NumeroSerie__c;
                    
                }
                OC_Equipement__c newEq = new OC_Equipement__c(id =e.id );
                equipToUpdate.add(newEq);
                equipToUpdate.add(e);
                // System.debug(e.EquipementReferentiel__c + ' = e.EquipementReferentiel__c value');
            }
        }  
        System.debug('*************'+equipToUpdate + ' = equipToUpdatevalue ******');
        //create a map that will hold the values of the equipToUpdate list 
        map<id,OC_Equipement__c> accmap = new map<id,OC_Equipement__c>();
        
        //put all the values from the list to map. 
        accmap.putall(equipToUpdate);
    }

    public static void updateParent(List<OC_Equipement__c> equipCreated) {
        List<id> listIdsCreatedEquip = new List<id>();
        for (OC_Equipement__c c : equipCreated) {
            system.debug('<========= '+c.EquipementReferentiel__c + ' c.EquipementReferentiel__c  value =========>');
            if(c.EquipementReferentiel__c != null) {
                listIdsCreatedEquip.add(c.EquipementReferentiel__c);   
            }
        }
        
        if(listIdsCreatedEquip.size()> 0) {
            System.debug('<================= listIdsCreatedEquip list value on SAVE() = ' + listIdsCreatedEquip + '=======>');
            Map<ID, OC_Equipement__c> parentEquip = new Map<ID, OC_Equipement__c>( [ SELECT ID, Statut__c from OC_Equipement__c where ID in :listIdsCreatedEquip ]);
            System.debug('<================= parentEquip MAP list value on SAVE() = ' + parentEquip + '=======>');
            OC_Equipement__c myParentEquip = new OC_Equipement__c();
            List<OC_Equipement__c> updateParents = new List<OC_Equipement__c>();
            
            if(parentEquip.size() > 0) {
                for(OC_Equipement__c childEquip : equipCreated) {
                    myParentEquip = parentEquip.get(childEquip.EquipementReferentiel__c);
                    myParentEquip.Statut__c = 'Présent et Utilisé';
                    System.debug('<================= myParentEquip list value on SAVE() = ' + myParentEquip + '=======>');
                    updateParents.add(myParentEquip);
                }
                runTrigger = false;
                update updateParents;
                System.debug('<================= updateParents list value on SAVE() = ' + updateParents + '=======>');
            } 
        }
    }
}

Here is the trigger itself : 
 
trigger OC_EquipementUpdateWithReferentiel on OC_Equipement__c (before insert, before update,after insert, after update) {
     String equipCollab='0120E000001bH9x';
     String equipReferentiel ='0120E000001bHA2';
    
     if (trigger.isBefore) {
        if(trigger.isInsert || trigger.isUpdate) {
           for(OC_Equipement__c e:trigger.new) {
                 if(e.RecordTypeId == equipReferentiel) {
                     OC_EquipementTRIGGER.UpdateEquipWithReferentiel(trigger.new);
                 } else if (e.RecordTypeId == equipCollab ) {
                     OC_EquipementTRIGGER.UpdateEquip(trigger.new);
                 }
           }
        } 
     } else {
         if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate)) {
             for(OC_Equipement__c e:trigger.new) {
                 if(e.RecordTypeId == equipReferentiel) {
                     OC_EquipementTRIGGER.UpdateEquipWithReferentiel(trigger.new);
                 } else if (e.RecordTypeId == equipCollab){
                     if(OC_EquipementTRIGGER.firstcall == true) {
                          OC_EquipementTRIGGER.firstcall = false;
                            OC_EquipementTRIGGER.updateParent(trigger.new);
                     } 
                 }
             }
         }
     }
}
There is a particularity in here : parent and child are on same object, but with different record type. 
When i create a child record and a parent is find, i need to update parent status to say it is used by someone. (in method update parent) that create this meessage error when i try to update a parent equipment. 
Anyone can help with that? 

Ty in advance :)  
 
AbhishekAbhishek (Salesforce Developers) 
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.

Try the suggestions as mentioned in the below blog,

https://stackoverflow.com/questions/7354302/salesforce-apex-error-self-reference-from-trigger


I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.


Thanks.