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
sgribisgribi 

Reroute case via assignment rules

We have a requirement to return cases to the default queue when a customer updates a case via comment / email and the current case owner is out-of-the-office. I don't seem to be able to call the assignment rules via APEX.

To reroute the queue, we created the following elements, but were not able to get it to work.
Process Builder triggers when a Case Comment is created, (Criteria being that case owner is out, and the comment is from a customer)
Process builder calls an APEX method that updates the relevant case with the assignmentRuleHeader.useDefaultRule DML option
While the Process Builder event fires (other updates occur such as Case status change), the case owner is not update per the default assignment rules.

We can run the same method via Execute Anonymous, and the case owner changes as expected.

This APEX code is intended to perform magic re-assignment. Logs indicate that the action executes, case id is passed along, and APEX update of the case succeeds; only reassignment does not occur. To rule out access issues, I wrapped the method in a without sharing class. Thus far no luck. Any other suggestions?
/*
* Methods to reroute a case to a queue/default owner via routing rules
*/
public class CaseReroute implements Queueable{
    /*
     * Invoke method from Process builder to reassign cases to queue
     * (see CaseController for reroute without notification)
	*/
    @InvocableMethod(label='Route to Default Queue with Notification' description='Reroutes a case to the queue via standard routing rule')
    public static void rerouteCaseToQueueWithEmail(list<id> caseIds)
    {
        //ID jobID = System.enqueueJob(new CaseReroute(caseIds, true));
        rerouteCaseToQueue(caseIds, true);
    }
    
    public list<id> recordIdList;
    public boolean sendNotification;
    /*
     * Instantiate object 
     * - with id list
     * - plain
	*/
    public CaseReroute(list<Id> caseIdList, boolean pSendNotification)
    {
        recordIdList = caseIdList;
        sendNotification = pSendNotification;
    }
    /*
     * Execute quable batch
	*/
    public void execute(QueueableContext context) {
        system.debug('CaseReroute: Execute for '+recordIdList.size()+' cases.');
        rerouteCaseToQueue(this.recordIdList, this.sendNotification);
    }
    // Add Enqueue method to make update asynchronous; simplify workflows to one vs. many
    public static void rerouteCaseToQueue(list<id> caseIds, boolean sendNotification)
    {
        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.assignmentRuleHeader.useDefaultRule = true;
        dmo.EmailHeader.triggerUserEmail = sendNotification==true;  // rev 3.1
        dmo.optAllOrNone = false;
        // Create case sobjects
        for(Case[] caseList: [Select Id From Case Where Id in: caseIds]) {
            // Update cases
            if(!caseList.isEmpty()) {
                database.SaveResult[] sr = database.update(caseList, dmo);
                system.debug('rerouteCaseToQueue: Save results:');
                system.debug(sr);
            }
        }
    }
        
}

 
sgribisgribi
One more piece of information: The behavior works as expected when a regular Salesforce user adds a case comment. However, when a customer adds a comment via customer portal, reassignment does not occur.