Skip to main content

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;

}

}

2 respostas
  1. 1 de mar. de 2021, 16:36
    When a lead is converted the status is simply Converted and not Closed - Converted. If you have added any custom values to this picklist so can you please make sure that the value is properly being updated to Closed - Converted or not? If Yes, then the last check would be to see the API name of this option. It might be possible that the label for the option is Closed - Converted but API name is different. So please make sure you are matching the value with API name and not label.

    Let me know if you need any other information on this.

    Thanks,

    Abhishek Bansal.
0/9000