Trigger assignment rules from apex

Automatically assign the leads or cases to users or queue using assignment rules when they are inserted via API or back-end apex logic.

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];
    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
    insert CS;


  • 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.