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
Snita LalSnita Lal 

Case Assignment Trigger Help

Hi,

We've started using quick actions to create Cases and we want an option, like you do in the Case edit screen, to choose whether to assign via assignment rules or not.

The below trigger pushes everything through the assignment rules even if created by the quick action, and due to the fact that I cannot include the assign via assignment rules checkbox on the quick action I have created a field called use_assignment_rules__c. I would therefore like to nest an IF statement into this trigger to only run the trigger when the checkbox is equal to true. Struggling to make this work could anyone advise on how to get the trigger to work, any help would be greatly appreciated.

Thank you,

Snita
 
trigger AssignmentRulesonCaseTrigger on Case (after insert) {
    List<Id> caseIds = new List<Id>{};
        if(trigger.IsAfter && trigger.isInsert){
            for (Case theCase:trigger.new) 
                caseIds.add(theCase.Id);
            
            List<Case> cases = new List<Case>{}; 
            for(Case c : [Select Id from Case where Id in :caseIds])
            {
                Database.DMLOptions dmo = new Database.DMLOptions();
     
                dmo.assignmentRuleHeader.useDefaultRule = true;
                c.setOptions(dmo);
                
                cases.add(c);
            
    }
            Database.upsert(cases);
        }
}

 
Best Answer chosen by Snita Lal
Ankit SehgalAnkit Sehgal
trigger AssignmentRulesonCaseTrigger on Case (after insert)
 {
    List<Id> caseIds = new List<Id>{};
        if(trigger.IsAfter && trigger.isInsert)
        {
            for (Case theCase:trigger.new) 
            {
                 if(theCase.use_assignment_rules__c==true)
                 {
                      caseIds.add(theCase.Id);
                 }
             }
            
            List<Case> cases = new List<Case>{}; 
            for(Case c : [Select Id from Case where Id in :caseIds])
            {
                Database.DMLOptions dmo = new Database.DMLOptions();
     
                dmo.assignmentRuleHeader.useDefaultRule = true;
                c.setOptions(dmo);
                
                cases.add(c);
            
            }
            Database.upsert(cases);
        }
}
Give this a try.