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
Snehal Gaware 15Snehal Gaware 15 

after update logic in trigger keeps on affecting because of before update

I have written a trigger on case object having before update and after update as it's events.
In Before update : Where user selects any value of "Assignment Group", based on that Case owner will get update to that particular Queue.
In After Update : Whenever Case owner changes to Queue, this event will perform auto assignment of case to queue members. Till this trigger is working as expected.
But issue occurs, when user wants to update any field on that case and save it AFTER UPDATE logic executes and case owner changed to new queue member of same queue. Assignment Group is same but case owner keeps on changing whenever user updates any value on case.

Trigger Code :
 
trigger caseOwnerUpdate on Case (before insert,before update,after update) 
{
    
   
  // 1.[Based on assignment group selection change the case owner] 
   
	if(Trigger.isBefore && Trigger.isUpdate)
    {
        System.debug('entered if condition');
		
        for(Case c : Trigger.new)
		{
       
            System.debug('Updating logic');
       	    List<Group> qid = [select Id from Group where Name = : c.Assignment_Group__c and Type = 'Queue'];
              for(Group g : qid)
                {
                    c.OwnerId = g.id;
                    System.debug('updated');
                }
        }        
    }
	
  // 2.based on assignment group(queue) perform autoassignment of case to queue members
   if(Trigger.isAfter && Trigger.isUpdate)
   {
	   
    List<Case> updateCS = new List<Case>();
    Map<Id,Case> cases = new Map<Id,Case>();
    
    for (Case cs : Trigger.new)
    {
        if(Trigger.isUpdate) {  
            System.debug('>>>>> Owner ID: '+cs.ownerId+' Temp Owner ID: '+cs.TempOwnerId__c);
            if(cs.TempOwnerId__c <> null && cs.TempOwnerId__c <> '') {
                if(cs.OwnerId <> cs.TempOwnerId__c) {
                    cases.put(cs.id,cs);
                }
            }           
        }   
    }
    if (cases.isEmpty()) return;
    
    for (Case cs : [SELECT OwnerId,TempOwnerId__c FROM Case WHERE id in :cases.keySet()]) {
        cs.OwnerId = cases.get(cs.Id).TempOwnerId__c;
        cs.TempOwnerId__c = 'SKIP'; //flag to stop infinite loop upon update
        updateCS.add(cs);
    }
    System.debug('>>>>>Update Cases: '+updateCS);
    
    //
    //Update last assignment for Assignment Group in batch
    //
    if (updateCS.size()>0) {
        try {
            update updateCS;
        } catch (Exception e){

        }
    }

   }
	
   // Entitlement
	if((Trigger.isBefore && Trigger.isUpdate)||(Trigger.isBefore && Trigger.isInsert))
    {       
    /*
   If the Entitlement Name is not set then, check to see if the Contact on the Case has an active Entitlement
    and select the first one.  If not then check to see if the Account on the Case has an active Entitlement.
   */
   List<Id> contactIds = new List<Id>();
   List<Id> acctIds = new List<Id>();
   for (Case c: Trigger.new){
      if (c.EntitlementId == null && c.ContactId!= null && c.AccountId!= null){
         contactIds.add(c.ContactId);
         acctIds.add(c.AccountId);
      }
   }
   if(contactIds.isEmpty()==false || acctIds.isEmpty()==false){
      /* Added check for active entitlement */
      List <EntitlementContact> entlContacts = [Select e.EntitlementId,e.ContactId,e.Entitlement.AssetId From EntitlementContact e
                                                Where e.ContactId in:contactIds
                                                And e.Entitlement.EndDate >= Today And e.Entitlement.StartDate <= Today];
      if(entlContacts.isEmpty()==false){
         for(Case c: Trigger.new){
            if(c.EntitlementId == null && c.ContactId!= null){
               for(EntitlementContact ec:entlContacts){
                  if(ec.ContactId==c.ContactId){
                     c.EntitlementId = ec.EntitlementId;
                     if(c.AssetId==null && ec.Entitlement.AssetId!=null)
                        c.AssetId=ec.Entitlement.AssetId;
                     break;
                  }
               } // end for
            }
         } // end for
      } else{
         List <Entitlement> entls = [Select e.StartDate, e.Id, e.EndDate, e.AccountId, e.AssetId
                                     From Entitlement e
                                     Where e.AccountId in:acctIds And e.EndDate >= Today And e.StartDate <= Today];
         if(entls.isEmpty()==false){
            for(Case c: Trigger.new){
               if(c.EntitlementId == null && c.AccountId!= null){
                  for(Entitlement e:entls){
                     if(e.AccountId==c.AccountId){
                        c.EntitlementId = e.Id;
                        if(c.AssetId==null && e.AssetId!=null)
                           c.AssetId=e.AssetId;
                        break;
                     }
                  } // end for
               }
            } // end for
         }
      }
   } // end if(contactIds.isEmpty()==false)
        
    }
       
 // based on assignment group assign value to 3 fields 
    
     if(Trigger.isBefore && Trigger.isUpdate)
    {
      for(Case c : Trigger.new)
		{   
			if(c.Assignment_Group__c=='Tech Support'||c.Assignment_Group__c=='GD-IT'||c.Assignment_Group__c=='App-Support'||c.Assignment_Group__c=='GD-RM'||c.Assignment_Group__c=='GD-DB'||c.Assignment_Group__c=='Dev-Ops'||c.Assignment_Group__c=='App-Management'||c.Assignment_Group__c=='PDT-DS-Engg'||c.Assignment_Group__c=='PDT-US-Engg')
            {
                c.Group_Manager_Email__c = 'sgaware148@gmail.com'; c.Escalation_Level_2_Email__c ='sgaware148@gmail.com'; c.Escalation_Level_3_Email__c='sgaware148@gmail.com';
            }
        }
    }
    
    
}

Please suggest.
Snehal Gaware 15Snehal Gaware 15
I have made the changes in after update as below
for (Case cs : Trigger.new)
    {
        if(Trigger.isUpdate) {  
            System.debug('>>>>> Owner ID: '+cs.ownerId+' Temp Owner ID: '+cs.TempOwnerId__c);
            if(cs.TempOwnerId__c <> null && cs.TempOwnerId__c <> '' && cs.OwnerId != trigger.oldMap.Get(cs.Id).OwnerId) {
                system.debug('pleasecheck'+trigger.oldMap.Get(cs.Id).OwnerId);
                if(cs.OwnerId <> cs.TempOwnerId__c) {
                    cases.put(cs.id,cs);
                }
            }           
        }   
    }
  
but still facing same issue. I am not changing assignment group but case owner keeps on changing. It should only change when i change assignment group field's value to queue.
DevADSDevADS
Hi Snehal,

Do you have any "Assignment Rules" running on the Case?, This could be a reason which is changing the owner on the case records.

Please confirm.

Happy Coding!
Snehal Gaware 15Snehal Gaware 15
Hi , Issue i'm getting is because of process builder. when i deactivate the process i am able to run test class successfully.