[SalesForce] caseTrigger, therefore it cannot recursively update itself

public class CaseOwnershipAssignment{

    public static void CaseOwner(List<Case> caselist){
   list<id> caseid = New list<id>();
   list<case> caseOwnerid = New list<case>();
   list<case> finalcase = New list<case>();

for(Case record: caselist) {
     caseid.add(record.id);
 }
   for(case record: [SELECT id,type,Account.type,priority,OwnerId from case WHERE id IN :caseid]) {
   caseOwnerid.add(record);
   }
     for(Case record: caseOwnerid) {

    IF((record.Priority == 'High' && ((record.Account.Type =='Prospect' || record.Account.Type =='Installation Partner'|| record.Account.Type =='Customer - Direct') 
          && (record.type == 'Electrical' || record.type == 'Structural'))) || (record.Priority =='Low' 
          &&((record.Account.Type =='Prospect' || record.Account.Type =='Installation Partner' || record.Account.Type =='Customer - Direct') && 
               (record.type == 'Mechanical' || record.type == 'Electronic')))){

               record.OwnerId = System.Label.RohanD;
               finalcase.add(record);
               system.debug('+++++++++++record.OwnerId1'+record.OwnerId);

               }else IF((record.Priority == 'Low' && ((record.Account.Type =='Prospect' || record.Account.Type =='Installation Partner'|| record.Account.Type =='Customer - Direct') 
          && (record.type == 'Electrical' || record.type == 'Structural'))) || (record.Priority =='High' 
          &&((record.Account.Type =='Prospect' || record.Account.Type =='Installation Partner' || record.Account.Type =='Customer - Direct') && 
               (record.type == 'Mechanical' || record.type == 'Electronic')))){

               record.OwnerId = UserInfo.getUserId();
               finalcase.add(record);
               system.debug('+++++++++++record.OwnerId2'+record.OwnerId);

               }

}

update finalcase;
  }

 }

Trigger:

Trigger caseTrigger on Case (before insert, before update, after insert, after update) {
  if(Trigger.isBefore){
          if(Trigger.isUpdate){
                  CaseOwnershipAssignment.CaseOwner(Trigger.new);
          }
          if(Trigger.isInsert){
                     //Code to execute before insert
          }
  }
  else if (Trigger.isAfter){
          if(Trigger.isUpdate){
                    //Code to execute after update
          }
          if(Trigger.isInsert){
                  //Code to execute after insert
          }
   }
}

I am getting the below error

Error:Apex trigger caseTrigger caused an unexpected exception, contact your administrator: caseTrigger: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 5006F000027U5uSQAS; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 5006F000027U5uS) is currently in trigger caseTrigger, therefore it cannot recursively update itself: []: Class.CaseOwnershipAssignment.CaseOwner: line 33, column 1

Can anyone please help me why this error coming and what's wrong with my code?

Best Answer

You just need to query the account records and update the values in caselist directly. Any changes made in a before-insert/before-update trigger will automatically be applied without a DML operation.


public class CaseOwnershipAssignment{
    public static void CaseOwner(List<Case> caselist){
        Map<Id, Account> accounts = new Map<Id, Account>();
        for(Case record: caseList) {
            accounts.put(record.AccountId, null);
        }
        accounts.putAll([SELECT Type FROM Account WHERE Id = :accounts.keySet()]);

        for(Case record: caselist) {
            String accountType = record.AccountId != null? accounts.get(record.AccountId).Type: null;
            IF((record.Priority == 'High' && ((accountType =='Prospect' || accountType =='Installation Partner'|| accountType =='Customer - Direct') 
                && (record.type == 'Electrical' || record.type == 'Structural'))) || (record.Priority =='Low' 
                &&((accountType =='Prospect' || accountType =='Installation Partner' || accountType =='Customer - Direct') && 
                    (record.type == 'Mechanical' || record.type == 'Electronic')))){
                    record.OwnerId = System.Label.RohanD;
                    system.debug('+++++++++++record.OwnerId1'+record.OwnerId);

                    }else IF((record.Priority == 'Low' && ((accountType =='Prospect' || accountType =='Installation Partner'|| accountType =='Customer - Direct') 
                && (record.type == 'Electrical' || record.type == 'Structural'))) || (record.Priority =='High' 
                &&((accountType =='Prospect' || accountType =='Installation Partner' || accountType =='Customer - Direct') && 
                    (record.type == 'Mechanical' || record.type == 'Electronic')))){
                    record.OwnerId = UserInfo.getUserId();
                    system.debug('+++++++++++record.OwnerId2'+record.OwnerId);
            }

            }

    }

}
Related Topic