4

In some situations I want to reassign an existing case to the default queue defined with the active assignment rules. However, I do not know how to reassign an existing case using assignment rules. I need to do this using a workflow rule or in Apex.

Does anyone have an idea how this can be done?

3

Here's what I found. There's a link to do this with apex programmatically, and you may need to call the assignmentRuleHeader class in an update trigger.

From a standard UI, a user can trigger assignment rules by simply checking the "Assign using active assignment rules" checkbox under the Optional section. The problem arises when your app needs to insert the Case from Apex and wants to trigger assignment rules. Using this script, a Case will be inserted but assignment rules will not be triggered as there is no such field "Assign using active assignment rules" on Case.

Code can be found here: http://developer.force.com/cookbook/recipe/running-case-assignment-rules-from-apex

https://help.salesforce.com/apex/HTViewSolution?id=000187821&language=en_US

I've taken the liberty of writing a simplified trigger and class with a method (having @future annotation --> rationale found here: Case Assignment Rule workaround). This concept can be extended to a Trigger on the User object, which can call the Case Reassignment method after a User is deactivated. Note, I intentionally did not bulkify the code to better demonstrate the concept.

In my coded example, I created a custom field [Reassign_with_Active_Assignment_Rule__c] (checkbox) to trigger the class to run and invoke the standard Case reassignment functionality.

trigger MyCaseReassignmentTrigger on Case (before update) {

    for(Case c : trigger.new){

        //only run trigger when my business condiions are met and not invoked by an @future method to avoid infinite recursion
        if(c.Reassign_with_Active_Assignment_Rule__c && !System.isFuture()){

            MyCaseReassignmentClass.reassignCaseWithActiveRule(c.Id);            

        }

    }

}




public class MyCaseReassignmentClass {


    /*
     * Pass the ID values to a future method, and perform your ownership changea synchronously.
     * This will allow you to reassign the user correctly after the assignment rule logic has executed.
     * Source: sfdcfox https://salesforce.stackexchange.com/a/40004/15114
     */

    @future
    public static void reassignCaseWithActiveRule(ID myCaseId) {

        //fetching the desired assignment rules on Case...
        AssignmentRule AR = new AssignmentRule();
        AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];

        //recreating the DMLOptions setting for "Assign using active assignment rules" checkbox on Case object...
        Database.DMLOptions dmlOpts = new Database.DMLOptions();
        dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;

        //get current instance of sObject (Case)...
        Case myCase = [select Id, Reassign_with_Active_Assignment_Rule__c from Case where Id =: myCaseId limit 1];

        //set DMLOptions on this record so it will be processed suing assignment rules...
        myCase.setOptions(dmlOpts);
        myCase.description = 'processed w/ @future on '+ dateTime.now();

        update myCase; 

    }

}
  • Thanks Jim, but this feature is related only to a newly created cases and I know how to deal with them. Unfortunately, I need to reassign an existing case when its owner becomes inactive for whatever reason. – Dmitry Nikolaev May 1 '15 at 17:40
  • @DmitryNikolaev, check my revised answer and let me know if this will work. I tested this in my org and it would reassign the case each time the method was called. – James May 4 '15 at 13:46
  • I did not think that this should work when updating a case as well. Thank you James, this is what I need! – Dmitry Nikolaev May 6 '15 at 9:00
  • @james Would you be able to provide test classes for the code you provided above? – Sarah H Dec 9 '16 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.