Skip to main content
SFDC Developers
Apex

Trigger assignment rules from apex

Vinay Vernekar · · 2 min read

Being a Salesforce Admin or Developer, I assume you have setup Assignment rules for your projects to auto assign cases or leads to either queue or user based upon some criteria.

The active assignment rules can be automatically triggered by checking the "Assign using active assignment rules" checkbox under the properties section of the lead or case page layouts. This option is only available if there are active assignment rules present on either lead or case.

Enable Assignment Rule Checkbox

Assignment Rule Checkbox on Layout

This automation will only applicable if the records is created from Salesforce UI and not through backend, which means through apex or REST API. There is no option in DML statements to assign the owners based on active assignment rules as of now. You need to manually specify active assignment rule to auto assign cases to users/groups.

Apex Code:

List<AssignmentRule> Rules = [select id from AssignmentRule where sObjectType = 'Case' and Active = true];
if(!Rules.isEmpty()){
    Database.DMLOptions DMLOpts = new Database.DMLOptions();
    DMLOpts.assignmentRuleHeader.assignmentRuleId = Rules[0].id;

    //declare case variable and assign values to fields
    Case CS = new Case(Status = 'New', Subject = 'Test Subject') ;

    //Setting the DMLOption on Case instance
    newCase.setOptions(DMLOpts);
    insert CS;
}

Explaination:

  • Instantiate Database.DMLOptions
  • User DMLOptions variable to assign assignment rule id queried by SOQL
  • Insert the case/lead record after configuration
  • If case/lead is matching the criteria of assignment rule, then it will automatically assigned to user or queue.

Share this article

Vinay Vernekar

Vinay Vernekar

Salesforce Developer & Founder

Vinay is a seasoned Salesforce developer with over a decade of experience building enterprise solutions on the Salesforce platform. He founded SFDCDevelopers.com to share practical tutorials, best practices, and career guidance with the global Salesforce community.

Get weekly Salesforce dev tutorials in your inbox

Comments

Loading comments...

Leave a Comment

Trending Now