function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

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 :{
    // Access the "old" record by its ID in Trigger.oldMap     
        if(l.status == 'Closed - Converted' || l.status == 'Closed - Not Converted'){
            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';
         system.debug('taskToUpdate = '+taskToUpdate);
    update taskToUpdate;
mary jon 7mary jon 7
Peryourhealth is medical billing portal. It provides patients secure access to their account. You can use to pay medical bills online. Peryourhealth may be a popular and trusted method to save lots of oneself from such agony. it's a time-saving and cashless billing platform.
Abhishek BansalAbhishek Bansal
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.

Abhishek Bansal.