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
Pete Watson 5Pete Watson 5 

Update Case Owner when Out of Office checkbox = true on user profile

Hi, firstly thanks for reading! 

I'm hoping someone can help - I need a trigger to update all cases owned by a user (to queue 'Aftersales') when a checkbox = true on a user profile (out_of_office__c). 

Any help would be greatly appreciated! 

Many thanks in advance 

 
ShirishaShirisha (Salesforce Developers) 
Hi Pete,

Greetings!

Here is the sample code which helps you and please make changes according to your bussiness logic:
 
Trigger CaseBefore on Case (before insert, before update){

//This will be your criteria
List<User> User = [select id from user limit 1];

for(Case cas : Trigger.new){
Cas.OwnerId = User[0].Id; // actual assignment will happend here.
}
Make sure to query the queue using the QueueSObject  class.

Also,there is an another approach which is to use the Process builder instead of the apex trigger to update the case owner to queue as per your logic.

Please check the steps and try it in your org:

https://www.soliantconsulting.com/blog/salesforce-process-builder/

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
 
AnudeepAnudeep (Salesforce Developers) 
Hi Pete, 

Triggers are not allowed on profile. If you attempt to write a trigger on profile, you will get the following error

SObject type does not allow triggers: profile

Assuming out_of_office__c is a checkbox field on the user, your code will look something like this
 
trigger ProfileTrigger on profile(before insert, before update) {
    List<Id> userIds = new List<Id>();
    List<Case> cases = new List<Case>();
    List<Case> casesToUpdate = new List<Case>();
    for(User u: Trigger.new) {
        if(u.out_of_office__c==True) {
            userIds.add(u.Id);
        }
      cases = [SELECT id, Description, queue__c, caseNumber FROM Case where ownerId IN:userIds];
      QueueSobject queue = [SELECT QueueId, Queue.Name, SobjectType FROM QueueSobject WHERE SobjectType = 'Case' and Queue.Name = 'Aftersales'];

        for(Case c:cases) {
            c.ownerId = queue.QueueId;
            casesToUpdate.add(c);
        }
        update casesToUpdate
    }
}

This is just a sample, please modify as it suits your requirement

Let me know if it helps

Anudeep
Footprints on the MoonFootprints on the Moon
Hi Pete,
You can use below pieces of code (Serves your purpose to assign Owner to "Aftersales" Queue, when current case owner is Out of Office)-
 
//UserTrigger.apxt

trigger UserTrigger on User (after update) {

    if(Trigger.isAfter){
        if(Trigger.isUpdate){
            UserTriggerHelper.updateCaseOwners(Trigger.new, Trigger.oldMap);
        }
    }
    
}
 
//UserTriggerHelper.apxc

public class UserTriggerHelper {

    public static void updateCaseOwners(List<User> updatedUserList, Map<Id, User> oldUserMap){
        
        Set<Id> userIdSet = new Set<Id>();
        for(User updatedUser : updatedUserList){

            //Filer out only those users who have been updated to be "Out of Office"
            if(updatedUser.Out_of_Office__c == true && oldUserMap.get(updatedUser.Id).Out_of_Office__c == false){
                userIdSet.add(updatedUser.Id);
            }
        }
        
        //Fetch "Aftersales" queue, to be assigned to Cases
        QueueSObject QueueID = [Select Queue.Id, Queue.Name, Queue.Type from QueueSObject WHERE Queue.Type ='Queue' AND Queue.Name = 'Aftersales' Limit 1];
        
        //Fetch all the cases having user who is now "Out of Office"
        List<Case> caseList = [select Id, OwnerId from Case where OwnerId in :userIdSet];
        
        for(Case caseToUpdate : caseList){
            //Assign case owner as "Aftersales" queue
            caseToUpdate.OwnerId = QueueID.Queue.Id;
        }
        
        update caseList;
        
    }
    
}
May the force be with you! 
 
Pete Watson 5Pete Watson 5
Thank you all very much for your help. I encountered a few issues with getting the queue Id (list kept returning empty) and ended up updating a checkbox on all open cases owned by the user. This checkbox then fired a process build to update the record to the aftersales queue. 

Thank you very much to both Anudeep and 'footprints on the moon'. Anudeep, the final variation of code used was very similar to yours but neither where able to return the queue id i'm afraid. 

Shirisha, thanks for your comment. The issue with process builder was that the record needed to be updated for the process to fire and thus needed to come from a trigger. But combining the two has worked well.