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
Wes McCarthyWes McCarthy 

Task trigger updating Lead record incorrectly.

Once i get the Task associated with my Lead record, i want to perform the following operations:

If Task is "A" and Status = Completed
   Update Lead Status to 'Contacted.'

If Task is "B" and Status = Completed and Lead Description field is Null
    Throw an error - 'Task cannot be completed while description is Null.
Else
    Update Lead Status to 'Qualified'.

The issue i've encountered is that when i complete Task "A", the Lead Status directly updates to 'Qualified'.  When i complete Task "B" and Lead Description is Null, nothing happens.  I dont receive an error message.  My code is below. 

Any suggestions are very welcome.

Thank you.



trigger LeadTrigger on Task (before insert, before update) {
    List<Id> leadIds=new List<Id>();
    for(Task t:trigger.new) {
        if(t.Status=='Completed') {
            if(t.whoId != null && 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
        
    map<id,Lead> LeadtaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]);
      
    for(Task tsk:trigger.new){
      if(LeadtaskMap.keyset().contains(tsk.whoid)) {
            if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead') {
                LeadtaskMap.get(tsk.whoid).Status = 'Contacted';
            }
            
            if(LeadtaskMap.get(tsk.whoid).Description == null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportuntity') {
                tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
            }
            Else {
                LeadtaskMap.get(tsk.whoid).Status = 'Qualified';
            }
      }    
    } 
    
    List<Lead> leadList = LeadtaskMap.values();
     update leadList;
    }
Best Answer chosen by Wes McCarthy
Wes McCarthyWes McCarthy
Ok.  I now have this working.  Code below.  Thanks for all of your help.  Much appreciated.

trigger LeadTrigger on Task (before insert, before update) 
{
    List<Id> leadIds=new List<Id>();
    for(Task t:trigger.new)
    {
        if(t.Status=='Completed')
        {
            if(t.whoId != null && 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
        
    map<id,Lead> LeadtaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]);
    
    
    
    for(Task tsk:trigger.new)
    {   
      if(LeadtaskMap.keyset().contains(tsk.whoid))
      {
            if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
            {
                LeadtaskMap.get(tsk.whoid).Status = 'Contacted';
            }
            
          //  System.debug('1 condition:' + LeadtaskMap.get(tsk.whoid).Description == NULL);
          //  System.debug('2 condition:' + tsk.Status == 'Completed');
          //  System.debug('3 condition:' + tsk.Subject == 'Make contact with Lead');
          //  System.debug('task:' + tsk);
            
            Else if(LeadtaskMap.get(tsk.whoid).Description == Null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Lead')
            {
                tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
            }
           Else 
            {
                LeadtaskMap.get(tsk.whoid).Status = 'Qualified';
           }
      }
    } 
    List<Lead> leadList = LeadtaskMap.values(); 
      update leadList;
      }

All Answers

Paul_BoikoPaul_Boiko
Hi Wes,
I saved this trigger in my dev org and tested when lead description = null and Status is 'Completed' and Subject 'Qualify Opportunity' and I got error message. Could you check if you select correct status and subject, also that Subject is spelled correctly. Also try to add System.debug before if statement and print all your conditions and you will see which condition is false in debug log.
System.debug('1 condition:' + LeadtaskMap.get(tsk.whoid).Description == null);
System.debug('2 condition:' + tsk.Status == 'Completed');
System.debug('3 condition:' + tsk.Subject == 'Other');
System.debug('task:' + tsk);

 
Wes McCarthyWes McCarthy

Thanks Paul.  I tried this, and got the following valdation error.  I have no idea what it means.  Any suggestions?
Thanks.

Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger LeadTrigger caused an unexpected exception, contact your administrator: LeadTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.LeadTrigger: line 21, column 1". 

Line 21 is the following line of code:
System.debug('1 condition:' + LeadtaskMap.get(tsk.whoid).Description == null);

Paul_BoikoPaul_Boiko
Wes, 
Are you testing with task related to lead? Also make sure that lead is not converted. 
Did you add system.debug before  if condition that you trying to test? like this:
 

System.debug('1 condition:' + LeadtaskMap.get(tsk.whoid).Description == null); 
System.debug('2 condition:' + tsk.Status == 'Completed'); 
System.debug('3 condition:' + tsk.Subject == 'Other'); System.debug('task:' + tsk);

if(LeadtaskMap.get(tsk.whoid).Description == null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportuntity') {
                tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
}

It does not make sense that it throws null object error if you already checked that leadTaskMap contains tsk.whoId.
Wes McCarthyWes McCarthy
Hi Paul,

Yep - am testing against a Task related to a Lead, and the Lead isnt converted.  I moved the debug code to just before the condition that im trying to test.  However, now i dont get anything at all.  The original issue is still there i.e. Lead status updating incorrectly.  This is very bizarre.
Paul_BoikoPaul_Boiko
Wes,
I just noticed that you have typo ("Qualify Opportuntity") instead of "Qualify Opportunity". Try changing it to "Qualify Opportunity" and test again.
Wes McCarthyWes McCarthy
Thanks Paul.  Good spot!  I have amended this, however, i still have the same issue.  I am at a loss.

trigger LeadTrigger on Task (before insert, before update) 
{
    List<Id> leadIds=new List<Id>();
    for(Task t:trigger.new)
    {
        if(t.Status=='Completed')
        {
            if(t.whoId != null && 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
        
    map<id,Lead> LeadtaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]);
    
    
    
    for(Task tsk:trigger.new)
    {   
      if(LeadtaskMap.keyset().contains(tsk.whoid))
      {
            if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
            {
                LeadtaskMap.get(tsk.whoid).Status = 'Contacted';
            }
            
            System.debug('1 condition:' + LeadtaskMap.get(tsk.whoid).Description == NULL);
            System.debug('2 condition:' + tsk.Status == 'Completed');
            System.debug('3 condition:' + tsk.Subject == 'Make contact with Lead');
            System.debug('task:' + tsk);
            
            if(LeadtaskMap.get(tsk.whoid).Description == NULL && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportunity')
            {
                tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
            }
           Else 
            {
                LeadtaskMap.get(tsk.whoid).Status = 'Qualified';
           }
      }
    } 
    List<Lead> leadList = LeadtaskMap.values(); 
      update leadList;
      }
Wes McCarthyWes McCarthy
Ok.  i have this half working.  I changed my 2nd condition to an Else If, and corrected a Typo on the Task Subject.
The Lead Status is now updating correctly.  However, I am not getting an error message when the Lead Description field is Null. Am i using the adderror function correctly?

Thanks
Wes McCarthyWes McCarthy
Ok.  I now have this working.  Code below.  Thanks for all of your help.  Much appreciated.

trigger LeadTrigger on Task (before insert, before update) 
{
    List<Id> leadIds=new List<Id>();
    for(Task t:trigger.new)
    {
        if(t.Status=='Completed')
        {
            if(t.whoId != null && 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
        
    map<id,Lead> LeadtaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]);
    
    
    
    for(Task tsk:trigger.new)
    {   
      if(LeadtaskMap.keyset().contains(tsk.whoid))
      {
            if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
            {
                LeadtaskMap.get(tsk.whoid).Status = 'Contacted';
            }
            
          //  System.debug('1 condition:' + LeadtaskMap.get(tsk.whoid).Description == NULL);
          //  System.debug('2 condition:' + tsk.Status == 'Completed');
          //  System.debug('3 condition:' + tsk.Subject == 'Make contact with Lead');
          //  System.debug('task:' + tsk);
            
            Else if(LeadtaskMap.get(tsk.whoid).Description == Null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Lead')
            {
                tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
            }
           Else 
            {
                LeadtaskMap.get(tsk.whoid).Status = 'Qualified';
           }
      }
    } 
    List<Lead> leadList = LeadtaskMap.values(); 
      update leadList;
      }
This was selected as the best answer
Paul_BoikoPaul_Boiko
Ok that's good! Yes you use addError method correctly.