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

Apex Trigger Help
I'm trying to trigger a Lead Status change to 'Contacted' upon the completion of a task (email or phone call). Here's what I have setup in the Task Triggers, but it's not working. Can anyone see why this isn't working?
trigger taskUpdateLeadStatus on Task (after update) {
list<lead> liLeads = new list<lead>();
list<id> liIds = new list<id>();
for(Task sTask : trigger.new)
{
if(sTask.Status == 'Completed' && sTask.Subject == 'Call')
{
liIds.add(sTask.WhoId);
}
}
for(Lead sLead : [select Id, Status from Lead where Id in : liIds])
{
sLead.Status = 'Contacted';
liLeads.add(sLead);
}
update liLeads;
}
Your trigger should work...
Your trigger is working on AFTER UPDATE, hope you are testing it on update of task....
May be you actually need it on after insert... otherwise it has no problem it should work
All Answers
You've specified "after update", but not "after insert", so this only works if they create the task and then modify it later. Change your code to include after insert. As a side note, your code has an extra query you could avoid:
Of course, if you wanted to restrict the updates to only when leads are in a certain status, then your query would be useful. I was simply noting that your particular code doesn't need a query since you're simply clobbering the value anyways.
Your trigger should work...
Your trigger is working on AFTER UPDATE, hope you are testing it on update of task....
May be you actually need it on after insert... otherwise it has no problem it should work
Ok, my Apex Trigger works properly, but I'm having trouble with my test. Below is by test method:
@isTest
private class taskUpdateLeadStatusTests {
static testMethod void validateUpdateLeadStatusWithCall() {
Lead l = new Lead (LastName='Phillips', Company='Phil Inc.');
l.Status = 'Open';
insert l;
Task t = new Task();
t.Subject = 'Call';
t.Status = 'Completed';
t.WhoId = l.Id;
insert t;
System.assertEquals(l.Status, 'Contacted');
}
static testMethod void validateUpdateLeadStatusWithEmail() {
Lead l = new Lead (LastName='Phillips', Company='Phil Inc.');
l.Status = 'Open';
insert l;
Task t = new Task();
t.Subject = 'Email';
t.Status = 'Completed';
t.WhoId = l.Id;
insert t;
System.assertEquals(l.Status, 'Contacted');
}
}
Here is the trigger associated with the test:
trigger taskUpdateLeadStatus on Task (after insert, after update) {
list<lead> liLeads = new list<lead>();
list<id> liIds = new list<id>();
for(Task sTask : trigger.new)
{
if(sTask.Status == 'Completed' && (sTask.Subject == 'Call' || sTask.Subject == 'Email'))
{
liIds.add(sTask.WhoId);
}
}
for(Lead sLead : [select Id, Status from Lead where Id in : liIds])
{
sLead.Status = 'Contacted';
liLeads.add(sLead);
}
update liLeads;
}
Can someone provide guidance on this topic?
Thanks,