How to trigger Assignment Rule when a Lead is created using Apex?

How to trigger Assignment Rule when a Lead is created using Apex?

assignmentRuleHeader.useDefaultRule should be set to true in Database.DMLOptions to fire Assignment Rule when a Lead is created using Apex.

Sample Code:

Lead objLead = new Lead(
	FirstName = 'Test1',
    LastName = 'Test2',
    Company = 'Testing',
    Email = '[email protected]'
);
Database.DMLOptions objDMLOptions = new Database.DMLOptions();
objDMLOptions.assignmentRuleHeader.useDefaultRule = true;
objLead.setOptions( objDMLOptions );
insert objLead;

Leave a Reply