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
John BraunJohn Braun 

Trigger that updates Lead Status based on any related, completed activity records

Hello all,

 

I recently came across a post on the success forums that contained a trigger that would update the lead status field on the lead record to "Working - Contacted", when an activity record had been added to the lead and was completed. 

 

I wanted to implement this trigger into our own org, however, for reasons I am hoping to get help with, the trigger also made it so you could not add activities on any other record (account, contact, opportunity, etc.).

 

Below is the trigger in question:

 

trigger changeLeadStatus on Task (before insert, before update) {
    String desiredNewLeadStatus = 'Working - Contacted';

    List<Id> leadIds=new List<Id>();
    for(Task t:trigger.new){
        if(t.Status=='Completed'){
            if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
                leadIds.add(t.whoId);
            }//if 2
        }//if 1
    }//for
    List<Lead> leadsToUpdate=[SELECT Id, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE];
    For (Lead l:leadsToUpdate){
        l.Status=desiredNewLeadStatus;
    }//for
    
    try{
        update leadsToUpdate;
    }catch(DMLException e){
        system.debug('Leads were not all properly updated.  Error: '+e);
    }
}//trigger

 

 

And here is the error message I receive when this trigger is active and I attempt to create task record against an account record:

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger changeLeadStatus caused an unexpected exception, contact your administrator: changeLeadStatus: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.changeLeadStatus: line 7, column 1

 

Does anyone know how I could go about modifying this trigger so that I still log activites elsewhere in Salesforce?

 



Best Answer chosen by Admin (Salesforce Developers) 
Rise AnalyticsRise Analytics

Try this. Replace:

 

    if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead

 with:

 

    if(t.whoId != null && String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead

 Let me know if that works.

 

All Answers

Rise AnalyticsRise Analytics

Try this. Replace:

 

    if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead

 with:

 

    if(t.whoId != null && String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead

 Let me know if that works.

 

This was selected as the best answer
John BraunJohn Braun

Rise Analytics,

 

That works perfectly, thank you!

 

Could you provide a brief explanation of the differences for educational purposes! Very appreciate of your help!

Rise AnalyticsRise Analytics

John,

 

My understanding is that since you are performing a function on a string, it is necessary that you first check that the string actually exists. Someone else may be able to chime in with details, but it's more a technical point than anything.

 

Dave

LawleywoodLawleywood

Does this not need a test class for coverage?