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
SteveOrg93SteveOrg93 

Trigger not creating a task on Lead

Hi There, I created a trigger to execute on a lead when the Lead Status value is changed.  I am able to save the code below, however the task is not being created.  I hardcoded the ownerID for the task for this test.   If anyone knows what I am missing this would be greatly appreciated!

 

trigger createLeadActivity on Lead (after update) { 

    //make a set to hold LeadIds that we need
    Set<Id> leadIds = new Set<Id>();
  
   
    for(Lead l : trigger.new)
        {
        //check to see if our field has been changed
        if(l.status != trigger.oldMap.get(l.Id).Status){
            
            leadIds.add(l.Id);
            
        }

       }
    
    if(!leadIds.isEmpty()){
        
       
        
        List<Lead> leadList = 
            [SELECT Id,Status
            FROM Lead
            WHERE Status = '4 - SAL Engaged' AND Id IN :leadIds ];
            
        
        
        
        
        for(Lead lead: trigger.new){
       
       
        
        
       
        Task newTask = new Task(Subject='LeadPassed',
Status='Not Started',Priority='Normal', OwnerId ='005E0000000QD4F');



        }
       
    }


}

 

Best Answer chosen by Admin (Salesforce Developers) 
Jake GmerekJake Gmerek
Correct you can loop through only the records that you want that way.

You can also relate the task to the lead by setting the WhoId on the task to the id of the lead.

All Answers

clouddev@surashriclouddev@surashri

Couldn't find insert task statement?

Jake GmerekJake Gmerek

You need to add:

 

insert newTask;

 

after you create the task.

SteveOrg93SteveOrg93

Thanks guys for the quick response!  I added:

 

insert newTask;

 

And I still can't see a task created when I modify a lead.    Here is the updated code: 

 

trigger createLeadActivity on Lead (after update) { 

    //make a set to hold LeadIds that we need
    Set<Id> leadIds = new Set<Id>();
  
   
    for(Lead l : trigger.new)
        {
        //check to see if our field has been changed
        if(l.status != trigger.oldMap.get(l.Id).Status){
            
            leadIds.add(l.Id);
            
        }

       }
    
    if(!leadIds.isEmpty()){
        
       
        
        List<Lead> leadList = 
            [SELECT Id,Status
            FROM Lead
            WHERE Status = '4 - SAL Engaged' AND Id IN :leadIds ];
            
        
        
        
        
        for(Lead lead: trigger.new){
       
       
        
        
       
        Task newTask = new Task(Subject='LeadPassed',
Status='Not Started',Priority='Normal', OwnerId ='005E0000000QD4F');

insert newTask;

        }
       
    }


}

 

 

Jake GmerekJake Gmerek
Where are you looking for the new task? You are not relating the task to any specific lead so it will not show up on the lead page. You should probably be able to see it on your home page.

On a related note:

1. Most of your trigger is not doing anything. You get the list of ids, query for the leads and then loop through the entire list of leads again. What that means is that if any of the leads passed to the trigger meet your criteria (i.e. l.status != trigger.oldMap.get(l.Id).Status) then ALL the leads passed to the trigger will have a task created, not just the ones that meet your criteria.
2. I did not notice that your task creation is inside of a for loop. That is bad. Once it is working we'll want to create a list of tasks and insert them all at once.
SteveOrg93SteveOrg93

Thanks for the reply.  

 

I was aiming to create the task for the leads that meet this condition after lead insert:

 

 List<Lead> leadList = 
[SELECT Id,Status
FROM Lead
WHERE Status = '4 - SAL Engaged' AND Id IN :leadIds ];

 

Would I be able to enable the task creation in this for loop like so?

 

for(Lead lead: leadList){

 

 

Regarding your last point, I will move the task creation outside the for loop. Thanks!

Jake GmerekJake Gmerek
Correct you can loop through only the records that you want that way.

You can also relate the task to the lead by setting the WhoId on the task to the id of the lead.
This was selected as the best answer