I have a scenario where i want to update the lead record's rating field when the status changes to status == 'Closed - Not Converted'.And And i wrote after update trigger for this:. Below is the code:
trigger leadUpdate on Lead (after update) {
list<lead> leadUpdate = new List<Lead>();
for(Lead l :trigger.new){
if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){
lead ld = new lead(id = l.id,rating = 'cold');
leadUpdate.add(ld);
}
}
update leadUpdate;
}
And samething can be achieved through before trigger as well. Below code:
And same thing can be achieved through before trigger as well. Below code:
trigger leadUpdate on Lead (before update) {
List<lead> leadUpdate = new List<Lead>();
if(trigger.isBefore && trigger.isUpdate){
for(Lead l :trigger.new){
if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){
l.rating = 'cold';
leadUpdate.add(l);
}
}
}
}
I want to ask which one is the correct approach and what is the difference?