You need to sign in to do that
Don't have an account?
SF7
Help with Trigger to Update a field on Lead Upon Task Creation
I want to update a field on Lead when a task is created for that lead .
What is the mistake i am doing ?
trigger LeadFollowUp on Task (after insert)
{
List<Lead> LeadsToUpdate = new List<Lead>();
for (Task t: Trigger.new)
{
if (t.subject<>'')
{
Lead l = new Lead(Id=t.WhoId);
l.Follow_up_done__c = true;
LeadsToUpdate.add(l);
}
}
try {
update LeadsToUpdate;
} catch (system.Dmlexception e) {
system.debug (e);
}
}
Thanks
Akhil
slightly revised to use the field names and if statement i think you want, though it's not clear to me from your original post
trigger updateLeadFromTask on Task (before insert, before update)
{
Map<Id,Lead> leadMap = new Map<Id,Lead>();
Set<id> Ids = new Set<id>();
for (Task tk: Trigger.new) {
Ids.add(tk.WhoId);
}
Map<id,Lead> leadMap2 = new Map<id,Lead>([Select Id, Lead_Test_Field__c from Lead Where Id in :Ids]);
for (Task t: Trigger.new)
if(t.Subject != null)
{
Lead l = leadMap2.get(t.WhoId);
l.Follow_up_done__c = true;
leadMap.put(l.id,l);
}
update leadMap.values();
}
All Answers
Hi Akhil
what is the error?
a point to think about, is that the Task object doesnt have to be related to lead (can be other objects too).
but you assume that by assigning the whatId to the "New" lead id.
this is wrong, you should first check that the what id is a lead id.
additionally, you trigger is not ready for bulk operations (what is two tasks are inserted for a lead?)
use this. can easily be edited with an if statement if you want. just swap out the test field names i use with your actual field names.
trigger updateLeadFromTask on Task (before insert)
{
Map<Id,Lead> leadMap = new Map<Id,Lead>();
Set<id> Ids = new Set<id>();
for (Task tk: Trigger.new) {
Ids.add(tk.WhoId);
}
Map<id,Lead> leadMap2 = new Map<id,Lead>([Select Id, Lead_Test_Field__c from Lead Where Id in :Ids]);
for (Task t: Trigger.new)
{
Lead l = leadMap2.get(t.WhoId);
l.Lead_Test_Field__c = t.Task_Test_Field__c;
leadMap.put(l.id,l);
}
update leadMap.values();
}
slightly revised to use the field names and if statement i think you want, though it's not clear to me from your original post
trigger updateLeadFromTask on Task (before insert, before update)
{
Map<Id,Lead> leadMap = new Map<Id,Lead>();
Set<id> Ids = new Set<id>();
for (Task tk: Trigger.new) {
Ids.add(tk.WhoId);
}
Map<id,Lead> leadMap2 = new Map<id,Lead>([Select Id, Lead_Test_Field__c from Lead Where Id in :Ids]);
for (Task t: Trigger.new)
if(t.Subject != null)
{
Lead l = leadMap2.get(t.WhoId);
l.Follow_up_done__c = true;
leadMap.put(l.id,l);
}
update leadMap.values();
}
Awesome that worked perfectly. Thanks Noam and SFAdmin5.
Just one thing what i am doing wrong before was not getting the task ids for the specific lead i am updating is that ri8?
Thanks
Akhil
IMPORTANT. my trigger above needs a mod to handle tasks created off records other than leads or contacts. use this
trigger updateLeadFromTask on Task (before insert, before update)
{
Map<Id,Lead> leadMap = new Map<Id,Lead>();
Set<id> Ids = new Set<id>();
for (Task tk: Trigger.new) {
Ids.add(tk.WhoId);
}
Map<id,Lead> leadMap2 = new Map<id,Lead>([Select Id, Lead_Test_Field__c from Lead Where Id in :Ids]);
for (Task t: Trigger.new)
if(t.Subject!=null && t.WhoId !=null)
{
Lead l = leadMap2.get(t.WhoId);
l.Follow_up_done__c = true;
leadMap.put(l.id,l);
}
update leadMap.values();
}
Thanks Ross Gilbert
sure. actually thanks to noam for reminding me about this important detail (see his post above). if you use the trigger you marked as resolved, it will fire on tasks generated from leads and contacts, but throw an error when you create any task off any other object. the revised trigger (the last one I've posted on this thread) should get around that error (i think, you should test as I have not done any testing on that trigger, esp bulk testing, though i think it should be fine).
Sure will test it out.
Thanks Noam and Ross
Hey Ross
have a problem with this trigger
when using only before insert my field on lead updates upon a task creation on leads and throws an exception while creating tasks on other objects .
and upon using before update only field does not update on leads when creating task on leads and throws an exception while updating tasks on other objects.
trigger updateLeadFromTask on Task (before insert)
{
Map<Id,Lead> leadMap = new Map<Id,Lead>();
Set<id> Ids = new Set<id>();
for (Task tk: Trigger.new) {
Ids.add(tk.WhoId);
}
Map<id,Lead> leadMap2 = new Map<id,Lead>([Select Id, Follow_up_done__c from Lead Where Id in :Ids]);
for (Task t: Trigger.new)
if(t.Subject != null&& t.WhoId !=null)
{
Lead l = leadMap2.get(t.WhoId);
l.Follow_up_done__c = true; // this is trhe line its throwing exception
leadMap.put(l.id,l);
}
try{
update leadMap.values();
}
catch (system.Dmlexception e) {
system.debug (e);
}
}
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.updateLeadFromTask: line 19, column 1
Thanks
Akhil
i jsut tested this out again by creating tasks from leads/opps/cases/accounts and have no problems with this trigger. can you tell me exactly how you are arriving at the error (step by step)
Hi Ross,
With before insert event
i am creating a tsk on lead and my field on lead is updating and when i create a task on contact it is throwing an exception.
Thanks
Akhil
oh yeah you're right
this should fix it
Oh i see so whats happening is every time i create a task on other object it is trying to find the lead field toupdate so we are using this lead prefix to solve that is that ri8?
Thank you so much for your help ross
Thanks
Akhil
that's correct. the whoid is a weird field that isn't looking at just one object but rather 2. actually we could technically get rid of that t.whoid!=null statement since it's not doing anything anymore, but it doesn't matter. pulling the object prefix for leads from the schema, and only firing the trigger on lead generated tasks, should limit this trigger to only fire on tasks on leads and not contacts