- trigger AutoAssignmentRuleTrigger on Lead (after insert, after update)
- {
- List<Lead> leadList = new List<Lead>();
- for(Lead currLead : Trigger.New)
- {
- if((Trigger.isInsert && Trigger.isAfter) && currLead.LeadScore__c != null)
- {
- leadList.add(new Lead(id = currLead.id));
- }
- else if((Trigger.isUpdate && Trigger.isAfter) && Trigger.oldMap.get(currLead.Id).LeadScore__c != currLead.LeadScore__c)
- {
- leadList.add(new Lead(id = currLead.Id));
- }
- }
- Database.DMLOptions dmo = new Database.DMLOptions();
- dmo.assignmentRuleHeader.useDefaultRule = true;
- Database.update(leadList, dmo);
- }
In the above trigger, default assignment rule has been been initiated by using the below line,
- dmo.assignmentRuleHeader.useDefaultRule = true;
You can also query the assignment rule and set that rule id in code as below,
- AssignmentRule AR = [select id from AssignmentRule where SobjectType = 'Lead' and Active = true AND Name = 'Cost Rule' limit 1];
- Database.DMLOptions dmo = new Database.DMLOptions();
- dmo.assignmentRuleHeader.assignmentRuleId = AR.Id;
- Database.update(leadList, dmo);
Reference:
No comments:
Post a Comment