How to Run Assignment Rules During Insert or Update Operation Using Apex In Salesforce?

How to Run Assignment Rules During Insert or Update Operation Using Apex In Salesforce?

In this blog, you will learn how you can run assignment rules during insert or update operation using apex in salesforce.


Assignment Rule During Insert Operation on Lead Object

AssignmentRule AR = new AssignmentRule();
AR = [select id from AssignmentRule where SobjectType = 'Lead' and Active = true limit 1];

Database.DMLOptions dmlOpts = new Database.DMLOptions();
dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;

Lead newLead = new Lead();
newLead.firstName = 'SFDC';
newLead.lastName ='SAGA';
newLead.company='SFDC SAGA';
newLead.status = 'New';
newLead.setOptions(dmlOpts);
insert newLead;


Assignment Rule During Update Operation on Lead Object

AssignmentRule AR = new AssignmentRule();
AR = [select id from AssignmentRule where SobjectType = 'Lead' and Active = true limit 1];

Database.DMLOptions dmlOpts = new Database.DMLOptions();
dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;

List<Lead> leadList = [SELECT Id From Lead LIMIT 5];
if(leadList.size()>0){
    Database.DMLOptions dmlOption = new Database.DMLOptions();
    dmlOption.assignmentRuleHeader.useDefaultRule = true;
    Database.update(leadList, dmlOption);
}


Demo


Follow Us

Posted By : Sudeer Kamat Date :

view_module Related

label Labels

Comments 0