-1

I have created custom label and assign one hardcoded id for testing purpose how we can achieve this using trigger on user (after update event)

1 Answer 1

2

First of all you can't directly update the Case to another active User using trigger on User Object this is a limitation from Salesforce.

Since User, Profiles are all setup object, you cannot have DML operation on non-setup object immediately after you have performed one on setup object.

To overcome this you will need to write a future method and from there you can make the updates on the Case Object.

Below is the code which is not compiled.

I am changing the Owner of the Case record if the earlier User is Inactive. You can change as per your requirement.

Apex Trigger

trigger Case_Assign_Queue on User (after update) {   
   for(User u : Trigger.New){
      if(u.IsActive == false){
        caseAssignToActiveUser.performOnAfterUpdate(u.Id);            
      }
   }
}

Handler Class

global class caseAssignToActiveUser{
@future
public static void performOnAfterUpdate(String deactiveUserId) {
    List<case> updatecaseList = new List<case>();      
    for(Case caseObj: [SELECT Id,OwnerId,Status 
                       FROM Case 
                       WHERE OwnerId =: deactiveUserId]) {
                           if(caseObj.Status != Label.Case_Status_Solved){
                               caseObj.OwnerId = Label.User_Id;
                               updatecaseList.add(caseObj);
                           }
                       }

    if(updatecaseList.size() > 0){
        try{  
            update updatecaseList;
        }Catch(Exception ex){
            system.debug('Exception------->'+ex);  
        }  
    }
}

}

3
  • i am trying and deactivated user its throw error Review all error messages below to correct your data. Apex trigger Case_Assign_Queue caused an unexpected exception, contact your administrator: Case_Assign_Queue: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 5009E000000rFscQAE; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Case, original object: User: []: Class.caseAssignToActiveUser.performOnAfterUpdate: line 23, column 1 Dec 30, 2016 at 11:42
  • You need to add @future anotation before the method
    – RCS
    Dec 31, 2016 at 13:57
  • if you are add @future then send the set of ids not list bcause list cannot support future and same thing you passed Trigger.New it will replace with keyset Jan 3, 2017 at 7:42

Not the answer you're looking for? Browse other questions tagged .