• Lawleywood
  • 0 Points
  • Member since 2013

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 1

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(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);



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?