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
Aaron HillAaron Hill 

Updating Lead Based on Task Subject

I've been trying to create a trigger for new tasks that are related to a lead. I've had a hard time explaining it in the past so before I post what I have so far I'll pseudo-code it.

//Trigger for when a new task is created
         //When this task is created on a lead
  
         //For (this new task) {

         //If the new task subject is equal to 'First Time Meeting (Phone or In-Person)'. {
                  //Change the custom field 'Had_First_Time_Meeting__c' to 'True'
         }
}
         //update the related lead
}


The task subject field is a picklist value. Here is the code that I created with the help of one of the other developers in the community.
 
trigger hadFirstMeeting on Task (before insert) {
    List<Lead> updatelead = new List<Lead>();
    for (task t : Trigger.new) {
        
        if (t.Type.contains('First Time Meeting (Phone or In-Person)')&& t.whatId!=null && t.whatId.getsobjectType() == Lead.sobjectType) {
            updatelead.add(new Lead(Id = t.whatId, Had_First_Time_Meeting__c  = true));
        }
    }
    
    update updatelead;
}

This task saves just fine and there are no errors when I create a new task with this subject. But it doesn't update the 'Had_First_Time_Meeting__c' field on the related lead to true. I have quadruple checked the merge fields to make sure they are accurate. I have no idea what to do. When I remove line 6 (The body of the if statement) and remove the second two requeirements from line 5 (&& t.whatId!=null && t.whatId.getsobjectType() == Lead.sobjectType). I can get it to update the task field . But other than that I'm a little lost.

I'd really appreciate any help.
Mahesh DMahesh D
Hi Aaron,

Please find the below code:

trigger hadFirstMeeting on Task (after insert) {
    List<Lead> updatelead = new List<Lead>();
    for (task t : Trigger.new) {
        
        if (t.Type.contains('First Time Meeting (Phone or In-Person)') && t.whatId != null && t.whatId.getsobjectType() == Lead.sobjectType) {
            updatelead.add(new Lead(Id = t.whatId, Had_First_Time_Meeting__c  = true));
        }
    }
    if(!updatelead.isEmpty()) {
        update updatelead;
    }
}
Aaron HillAaron Hill
Mahesh,

Thanks for getting back to me. It still didn't update the lead field. 
Mahesh DMahesh D
Could you please print all values using the System.debug();

like below

System.debug('-----------------t.Type:'+t.Type);
System.debug('-----------------t.whatId:'+t.whatId);
System.debug('-----------------t.whatId.getsobjectType():'+t.whatId.getsobjectType());

Also print the value of "updatelead" after the for loop.
and provide the results.
venkat-Dvenkat-D
HI Aaron,
Try with WHoId rather tahn whatid if you have leads attached under whoid
Deepak GulianDeepak Gulian
trigger hadFirstMeeting on Task (before insert) {
    List<Lead> updatelead = new List<Lead>();
    for (task t : Trigger.new) {
        
        if (t.Type.contains('First Time Meeting (Phone or In-Person)')&& t.whoId!=null && t.whoId.getsobjectType() == Lead.sobjectType) {
            updatelead.add(new Lead(Id = t.whoId, Had_First_Time_Meeting__c  = true));
        }
    }
    
    update updatelead;
}
Try this!
Aaron HillAaron Hill
That worked great Venky and Deepak, thank you. Now I'm writing the test class.