I am pretty new to writing custom code in SF and would appreciate any help. I have a trigger on Leads to update Status to "Contacted" when a task on that Lead is "Completed". I found this in a similar post on Stack Exchange and made some modifications to fit my org. Trigger and test class below:

trigger changeLeadStatus on Task (before insert, before update) {

    String desiredNewLeadStatus = 'Contacted';
    List<Id> leadIds=new List<Id>();
    for(Task t:trigger.new){
            if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
            }//if 2
        }//if 1
    List<Lead> leadsToUpdate=[SELECT Id, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE];
    For (Lead l:leadsToUpdate){

        update leadsToUpdate;
    }catch(DMLException e){
        system.debug('Leads were not all properly updated.  Error: '+e);

Test Class:

private class LeadStatusTest {

    static testMethod void testTaskCompletionEvent(){
        // Create a lead record
        Lead newLead = new Lead() ; 
        newLead.LastName = 'Yang' ; 
        newLead.Company = 'Freedom Dividend' ; 
        newLead.Status = 'Prospect' ;
        insert newLead;     

        // Create a task record
        Task t = new Task() ; 
        t.OwnerId = UserInfo.getUserId() ; 
        t.Subject = 'Cash for all' ; 
        t.Status = 'Completed' ; 
        t.Priority = 'High' ; 
        t.whoid = newLead.id ; 
        insert t ; 

The trigger has been deployed and is working in Production, with the exception of one use case. Some of our Users keep track of their activities by BCC'ing SF. In return, they are receiving the message below:

enter image description here

Any insight to why this happening or what I can do to fix this would be appreciated. Thanks

  • 2
    Probably not a solution to the error, but the code if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){ would be far better as if (t.whoId != null && t.whoId.getSObjectType() == Lead.SObjectType) {
    – Phil W
    Mar 27, 2020 at 20:05
  • @Brandon Just a side comment: even if the trigger is "technically" working, it should have been on after insert and after update events, because you need to ensure tasks are saved in database before processing logic on other records (lead). You can read more about this here and it is also a best practice to keep trigger logic-less by calling an apex class to perform the main logic. Read more about trigger best practices here
    – Shamina
    Mar 28, 2020 at 13:38

1 Answer 1


You can have a Task where WhoId is blank. In that case, you would get the error mentioned.

Instead of:



if (t.WhoId != null && t.WhoId.getSObjectType() == Lead.sObjectType)

Please note that you should never compare a Boolean to true, neither in code nor in formulas.

  • Lead.SObjectType?
    – Phil W
    Mar 27, 2020 at 20:07
  • 1
    Just goes to show why this approach is so much better than prefix comparison.
    – Adrian Larson
    Mar 27, 2020 at 20:07
  • Yours be better... sobject type wins :) Mar 27, 2020 at 20:08
  • I totally agree with it - see my comment on the question...
    – Phil W
    Mar 27, 2020 at 20:08
  • Thanks @AdrianLarson. Noted on comparing Boolean to true/false. Mar 27, 2020 at 20:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .