You need to sign in to do that
Don't have an account?
Aaron Hill
Updating Lead Based on Task Subject
I've been trying to create a trigger for new tasks that are related to a lead. I've had a hard time explaining it in the past so before I post what I have so far I'll pseudo-code it.
//Trigger for when a new task is created
//When this task is created on a lead
//For (this new task) {
//If the new task subject is equal to 'First Time Meeting (Phone or In-Person)'. {
//Change the custom field 'Had_First_Time_Meeting__c' to 'True'
}
}
//update the related lead
}
The task subject field is a picklist value. Here is the code that I created with the help of one of the other developers in the community.
This task saves just fine and there are no errors when I create a new task with this subject. But it doesn't update the 'Had_First_Time_Meeting__c' field on the related lead to true. I have quadruple checked the merge fields to make sure they are accurate. I have no idea what to do. When I remove line 6 (The body of the if statement) and remove the second two requeirements from line 5 (&& t.whatId!=null && t.whatId.getsobjectType() == Lead.sobjectType). I can get it to update the task field . But other than that I'm a little lost.
I'd really appreciate any help.
//Trigger for when a new task is created
//When this task is created on a lead
//For (this new task) {
//If the new task subject is equal to 'First Time Meeting (Phone or In-Person)'. {
//Change the custom field 'Had_First_Time_Meeting__c' to 'True'
}
}
//update the related lead
}
The task subject field is a picklist value. Here is the code that I created with the help of one of the other developers in the community.
trigger hadFirstMeeting on Task (before insert) { List<Lead> updatelead = new List<Lead>(); for (task t : Trigger.new) { if (t.Type.contains('First Time Meeting (Phone or In-Person)')&& t.whatId!=null && t.whatId.getsobjectType() == Lead.sobjectType) { updatelead.add(new Lead(Id = t.whatId, Had_First_Time_Meeting__c = true)); } } update updatelead; }
This task saves just fine and there are no errors when I create a new task with this subject. But it doesn't update the 'Had_First_Time_Meeting__c' field on the related lead to true. I have quadruple checked the merge fields to make sure they are accurate. I have no idea what to do. When I remove line 6 (The body of the if statement) and remove the second two requeirements from line 5 (&& t.whatId!=null && t.whatId.getsobjectType() == Lead.sobjectType). I can get it to update the task field . But other than that I'm a little lost.
I'd really appreciate any help.
Please find the below code:
trigger hadFirstMeeting on Task (after insert) {
List<Lead> updatelead = new List<Lead>();
for (task t : Trigger.new) {
if (t.Type.contains('First Time Meeting (Phone or In-Person)') && t.whatId != null && t.whatId.getsobjectType() == Lead.sobjectType) {
updatelead.add(new Lead(Id = t.whatId, Had_First_Time_Meeting__c = true));
}
}
if(!updatelead.isEmpty()) {
update updatelead;
}
}
Thanks for getting back to me. It still didn't update the lead field.
like below
System.debug('-----------------t.Type:'+t.Type);
System.debug('-----------------t.whatId:'+t.whatId);
System.debug('-----------------t.whatId.getsobjectType():'+t.whatId.getsobjectType());
Also print the value of "updatelead" after the for loop.
and provide the results.
Try with WHoId rather tahn whatid if you have leads attached under whoid