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
JGillespieJGillespie 

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

Best Answer chosen by Admin (Salesforce Developers) 
Yoganand GadekarYoganand Gadekar

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

sfdcfoxsfdcfox

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:

 

trigger taskUpdateLeadStatus on Task(after insert, after update) {
  Map<Id, Lead> leads = new Map<Id, Lead>();
  for(Task record:Trigger.new) {
    if(record.WhoId!=null && record.WhoId.getSobjectType()==Lead.SObjectType && record.Status=='Completed' && record.Subject=='Call') {
      leads.put(record.whoid, new lead(id=whoid,Status='Contacted'));
    }
  }
  update leads.values();
}

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.

Yoganand GadekarYoganand Gadekar

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

This was selected as the best answer
JGillespieJGillespie

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,