0

I am working on the Custom Trigger and getting the following error:

ERROR Error: Update failed. First exception on row 0 with id a01c000000A8GvrAAF; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, XXXXTrigger: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a01c000000A8GvrAAF; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = a01c000000A8Gvr) is currently in trigger XXXXXTrigger, therefore it cannot recursively update itself: [] Class.XXXClass.XXXX: line 41, column 1 Trigger.XXXTrigger: line 3, column 1: []

I have the following code

trigger XXXTrigger on ServiceXXXX__c (before insert, before update, after insert, after update){
    XXXX.XXXX(Trigger.New)
}

Here is the helper class:

public class XXXX {
    public static void XXX(List<SObject> newSobjs){

        List<ServiceXXXX__c> newSI = (List<ServiceXXXX__c>) newSobjs;       
        List<ServiceXXXX__c> testlist = new List<ServiceXXXX__c>();

        Set<Id> caseIdSet = new Set<Id>();
        for(ServiceXXXX__c si : newSI){
            caseIdSet.add(si.Case__c);
        }

         Map<Id,Case> caseMap = new Map<Id,Case>([SELECT Id,Category,SubCategory
                                               FROM Case 
                                              WHERE Id IN :caseIdSet]);      

        for(ServiceXXXX__c si : newSI){
            if(caseMap.get(si.Case__c).Category__c == 'YYYY'){
                if(caseMap.get(si.Case__c).SubCategory__c == 'ZZZZ'){

                    if(si.XXX && si.XXX != null){
                        ServiceXXXX__c newSInfo = new ServiceXXXX__c();
                        newSInfo.Id = si.Id;
                        if(si.CCC == null){
                            newSInfo.CCC = 0;
                        }

                        if(newSInfo.CCCC > 0 && newSInfo.CCC <=2){
                            newSInfo.CCC++;
                        }
                        testlist.add(newSInfo);
                        System.debug('COUNTER :::==> '+newSInfo.CCC);
                    }
                }     
            }
        }
        update testlist;
    }
}
  • 6
    :D that's a lot of obfuscation. Are you working for the CIA? The error speaks for itself. You're updating a record which is already sent in the trigger. You should call your function only on the before event. Then, you'll be able to modifiy some fields values without calling the update DML statement. – Martin Lezer Aug 30 '17 at 13:42
  • I agree with Martin here, the amount of obfuscation is off-putting, and makes the code quite hard to read. – Derek F Aug 30 '17 at 13:44
  • No I am not working on CIA. Its a trigger logic only. When case is updating the MasterDetails relationship with Service object, service object trigger is causing a problem. I have paste whole code in the snippet. – user46742 Aug 30 '17 at 13:54
  • Surely one of the indicated duplicates or the results of a search: salesforce.stackexchange.com/… will enlighten you – Eric Aug 30 '17 at 14:23