You need to sign in to do that
Don't have an account?
Upton_X
Writing a Trigger when Lead is Closed
Hello, I am writing a trigger with the following requirements:
Write a trigger that automatically sets Tasks to the “Completed” status whenever their associated Leads are set to any Closed status. Make sure your trigger is bulkified - there should be no SOQL queries inside any loops.
I have written the following trigger - it works well if the task is 'Closed - Not Converted' however when I convert the lead, the task is not closing. Can you provide some guidance please?
trigger SetTaskCompletedOnLeadClosed on Lead (after update) { //Create a set of ideas for no duplicates Set<Id> leadListIds = new Set<Id>(); for(Lead l : Trigger.new){ // Access the "old" record by its ID in Trigger.oldMap if(l.status == 'Closed - Converted' || l.status == 'Closed - Not Converted'){ leadListIds.add(l.id); system.debug('LeadListIds = '+leadListIds); } } List<Task> taskList = [SELECT id, subject, status, whoid FROM Task WHERE whoid in :leadListIds]; system.debug('taskList = '+taskList); List<Task> taskToUpdate = new List<Task>(); for(Task t : taskList){ if(t.status != 'Completed'){ t.status = 'Completed'; } taskToUpdate.add(t); system.debug('taskToUpdate = '+taskToUpdate); } if(taskToUpdate.size()>0){ update taskToUpdate; } }
Let me know if you need any other information on this.
Thanks,
Abhishek Bansal.