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.
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.
Leave a Reply