Skip to main content
New tool CRON Expression Builder — preview next run times before you schedule Apex. Open the builder →
Diagram illustrating how to trigger assignment rules automatically using Apex code logic
Apex

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.

The short answer

Standard DML operations in Apex do not trigger Case or Lead assignment rules by default. To execute active assignment rules programmatically, configure Database.DMLOptions with the active AssignmentRule ID and attach it to the record using setOptions() before inserting.

Key takeaways Query the active AssignmentRule record using SOQL to retrieve its ID for Case or Lead routing. Instantiate Database.DMLOptions and assign the rule ID to assignmentRuleHeader.assignmentRuleId. Attach the Database.DMLOptions instance to your record using setOptions() before running the insert DML operation. Verify that inserted records meet assignment rule criteria to ensure automatic routing to the appropriate user or queue.

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.

Frequently asked questions

How do you trigger assignment rules in Apex?

Query the active AssignmentRule ID using SOQL and assign it to assignmentRuleHeader.assignmentRuleId within a new Database.DMLOptions instance. Apply these options to your Case or Lead record with setOptions() before inserting the record.

Do assignment rules run automatically when creating records in Apex?

No, assignment rules only run automatically through the Salesforce user interface when configured on page layouts. Records created through backend code such as Apex or the REST API require explicit configuration using Database.DMLOptions.

Newsletter

One email every Tuesday

New guides, tool updates, and the release-note changes that break things.

No spam. Unsubscribe in one click.

Comments

Loading comments...

Leave a Comment