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 

Issue with Lead Task Trigger

Hi there,

I am trying to run a Task trigger on a Lead as follows.  
If Task Subject is A, then update the Lead Status upon Closure of Task.
If Task Subject is B, then check value of Description field on Lead.  
     If null, return an error.  
     If not null, then update the Status of the Lead upon closure of the Task.

Code is below, however it doesnt seem to be working.  Would appreciate any advice.  

Many thanks

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.whatid))
      {
            if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
            {
                LeadtaskMap.get(tsk.whatid).Status = 'Contacted';
            }
            
            if(LeadtaskMap.get(tsk.whatid).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.whatid).Status = 'Qualified';
            }
      }
        
    } 
    
Best Answer chosen by Wes McCarthy
Paul_BoikoPaul_Boiko
That's weird error. I was able to save this trigger by commenting out this line
update leadList;
and then after it saved I uncommented this line and was able to save trigger.  

All Answers

MandyKoolMandyKool
I guess the issue is with below code - 

if(LeadtaskMap.keyset().contains(tsk.whatid))

I think you should use tsk.whoid - As you queried the leads based on whoIds.
  
Wes McCarthyWes McCarthy
Thanks.  I have tried that, but still not working.  New code below.

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';
            }
      }
        
    } 
    
    }
Paul_BoikoPaul_Boiko

I don't see where you update leads in this code.
Try adding this code as a last line of code in trigger: 1 List<Lead> leads = LeadtaskMap.values();
 
List<Lead> leads = LeadtaskMap.values();
​update leads;



 
Wes McCarthyWes McCarthy
Thanks.  I have tried to add that code, but get the following complie error:  

Error: Compile Error: line 40:4 no viable alternative at character '​' at line 40 column 4
Wes McCarthyWes McCarthy
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;
    
    }
Paul_BoikoPaul_Boiko
That's weird error. I was able to save this trigger by commenting out this line
update leadList;
and then after it saved I uncommented this line and was able to save trigger.  
This was selected as the best answer
Wes McCarthyWes McCarthy
Thanks Paul.  That seemed to do the trick re: compilation error, however, code is still not doing what i want it to.  It seems to go into the final else clause.

the adderror function does not appear to be working.