I am trying to invoke Lead assignment rule when ever certain criteria meets the trigger should fire.
But I am getting the following error :
execution of AfterInsert caused by: System.DmlException: Update
failed. First exception on row 0; first error: MISSING_ARGUMENT, Id
not specified in an update call: []:
Can anyone guide me where I am donig wrong.Thanks in advance.
My Trigger:
trigger LeadAssignment on Lead (After insert,After update) {
List<Lead> updateLeads = new List<Lead>();
Lead newLead = new Lead();
for(Lead l : trigger.new)
{
if(l.Status == '01-New' || l.Status == '50-Qualified')
{
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;
system.debug('dmlOpts***********'+dmlOpts);
newLead.setOptions(dmlOpts);
// updateLeads.add(newLead);
}
}
update newLead;
system.debug('updateLeads************'+newLead);
}
Best Answer
You are trying to update
Lead newLead = new Lead()
, you didn't give it an ID so it doesn't know who it needs to update. You would have to do something likenewLead = new Lead(Id=l.id)
inside the loop;But in general, i think you need to bulkify your code. Currently you will only do the update on one lead, and it will be the last lead in the trigger.