You need to sign in to do that
Don't have an account?

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'); } } }
You can also relate the task to the lead by setting the WhoId on the task to the id of the lead.
All Answers
Couldn't find insert task statement?
You need to add:
insert newTask;
after you create the task.
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:
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.
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!
You can also relate the task to the lead by setting the WhoId on the task to the id of the lead.