You need to sign in to do that
Don't have an account?
DannyTK
Trigger to update Lead field from Task field
Hi community,
I'm not a developer, but was working on a trigger that will update a field on the Lead Object (Trigger_Email_on_Outstanding_Task__c (checkbox) on Lead) when the corresponding field on the related task is updated (Trigger_Email_on_Outstanding_Task__c (checkbox) on Task). This is due to the limitation of not being able to create cross-object workflows. So if the custom field is checked on the Task, then it'll update the custom field on the Lead.
Seems simple enough, but again, I'm not a developer so even the simple codes can seem difficult:
What i have is below:
Trigger TriggerTaskNotification on Task (after insert, after update) {
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new){
if(t.Trigger_Email_on_Outstanding_Task__c==true){
if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
leadIds.add(t.whoId);
}//if 2
}//if 1
}//for
List<Lead> leadsToUpdate=[SELECT Id, Trigger_Email_on_Outstanding_Task__c FROM Lead WHERE Id IN :leadIds AND Trigger_Email_on_Outstanding_Task__c= True];
For (Lead l:leadsToUpdate){
}//for
try{
update leadsToUpdate;
}catch(DMLException e){
system.debug('Leads were not all properly updated. Error: '+e);
}
}
Doesn't seem to work, can someone point me to the fix for this, it's all greek to me.
I'm not a developer, but was working on a trigger that will update a field on the Lead Object (Trigger_Email_on_Outstanding_Task__c (checkbox) on Lead) when the corresponding field on the related task is updated (Trigger_Email_on_Outstanding_Task__c (checkbox) on Task). This is due to the limitation of not being able to create cross-object workflows. So if the custom field is checked on the Task, then it'll update the custom field on the Lead.
Seems simple enough, but again, I'm not a developer so even the simple codes can seem difficult:
What i have is below:
Trigger TriggerTaskNotification on Task (after insert, after update) {
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new){
if(t.Trigger_Email_on_Outstanding_Task__c==true){
if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
leadIds.add(t.whoId);
}//if 2
}//if 1
}//for
List<Lead> leadsToUpdate=[SELECT Id, Trigger_Email_on_Outstanding_Task__c FROM Lead WHERE Id IN :leadIds AND Trigger_Email_on_Outstanding_Task__c= True];
For (Lead l:leadsToUpdate){
}//for
try{
update leadsToUpdate;
}catch(DMLException e){
system.debug('Leads were not all properly updated. Error: '+e);
}
}
Doesn't seem to work, can someone point me to the fix for this, it's all greek to me.
find the below code
Trigger TriggerTaskNotification on Task (after insert, after update) {
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new){
if(t.Trigger_Email_on_Outstanding_Task__c==true){
if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
if( !leadIds.contains(t.whoid)){ //Dont forget '!'
leadIds.add(t.whoId);
}//if3
}//if 2
}//if 1
}//for
/* List<Lead> leadsToUpdate=[SELECT Id, Trigger_Email_on_Outstanding_Task__c FROM Lead WHERE Id IN :leadIds AND Trigger_Email_on_Outstanding_Task__c= True]; */
Map<Id,Lead> relatedleads = new Map<Id,Lead>(SELECT Id, Trigger_Email_on_Outstanding_Task__c FROM Lead WHERE Id IN :leadIds);
For(Task t2 : trigger.new){
if(t2.Trigger_Email_on_Outstanding_Task__c==true){
if(relatedleads.get(t2.whoid) != null){ //if task is related to lead
//make lead field to 'checked'
relatedleads.get(t2.whoid).Trigger_Email_on_Outstanding_Task__c = true;
}//if2
}//if1
}//for
try{
update relaredleads.values();
}catch(DMLException e){
system.debug('Leads were not all properly updated. Error: '+e);
}
}
-Thank you
You can choose it as best answer, if it resolved your issue.
All Answers
Please provide the complete code which might help us understand the issue better. Also please let us know if you are getting any error?
Try this (check the bold)
Trigger TriggerTaskNotification on Task (after insert, after update) {
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new){
if(t.Trigger_Email_on_Outstanding_Task__c==true){
if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
if( !leadIds.contains(t.whoid)){ //Dont forget '!'
leadIds.add(t.whoId);
}//if3
}//if 2
}//if 1
}//for
/*
List<Lead> leadsToUpdate=[SELECT Id, Trigger_Email_on_Outstanding_Task__c FROM Lead WHERE Id IN :leadIds AND Trigger_Email_on_Outstanding_Task__c= True]; */Map<Id,Lead> relatedleads = new Map<Id,Lead>(SELECT Id, Trigger_Email_on_Outstanding_Task__c FROM Lead WHERE Id IN :leadIds);
For(Task t2 : trigger.new){
if(t.Trigger_Email_on_Outstanding_Task__c==true){
if(relatedleads.get(t.whoid) != null){ //if task is related to lead
//make lead field to 'checked'
relatedleads.get(t.whoid).Trigger_Email_on_Outstanding_Task__c = true;
}//if2
}//if1
}//for
try{
update relaredleads.values();
}catch(DMLException e){
system.debug('Leads were not all properly updated. Error: '+e);
}
}
-Thank you
You can choose it as best answer, if it resolved your issue.
Ramu, you're correct in that it's not throwing an error, but the function of updating the checkbox on lead isn't working when the task box is checked.
Chidambar, i'm trying to incorporate your suggestion, doesn't seem to recognize the variable (t.whoid) on lines 21 and 24. I moved the ")" after the null on line 21 and it seems to work for 21, but line 24:
relatedleads.get(t.whoid).Trigger_Email_on_Outstanding_Task__c = true;
it still doesn't recognize the (t.whoid) variable.....it appears that the reference is there, so not sure why it's throwing the Variable does not exist error for this. any further suggestions would be appreciated.
thanks guys
find the below code
Trigger TriggerTaskNotification on Task (after insert, after update) {
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new){
if(t.Trigger_Email_on_Outstanding_Task__c==true){
if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
if( !leadIds.contains(t.whoid)){ //Dont forget '!'
leadIds.add(t.whoId);
}//if3
}//if 2
}//if 1
}//for
/* List<Lead> leadsToUpdate=[SELECT Id, Trigger_Email_on_Outstanding_Task__c FROM Lead WHERE Id IN :leadIds AND Trigger_Email_on_Outstanding_Task__c= True]; */
Map<Id,Lead> relatedleads = new Map<Id,Lead>(SELECT Id, Trigger_Email_on_Outstanding_Task__c FROM Lead WHERE Id IN :leadIds);
For(Task t2 : trigger.new){
if(t2.Trigger_Email_on_Outstanding_Task__c==true){
if(relatedleads.get(t2.whoid) != null){ //if task is related to lead
//make lead field to 'checked'
relatedleads.get(t2.whoid).Trigger_Email_on_Outstanding_Task__c = true;
}//if2
}//if1
}//for
try{
update relaredleads.values();
}catch(DMLException e){
system.debug('Leads were not all properly updated. Error: '+e);
}
}
-Thank you
You can choose it as best answer, if it resolved your issue.
looks like it's giving a compile error: Method does not exist or incorrect signature : [List <id>].contains(id)
on line 8
if( !leadIds.contains(t.whoid))
thoughts?
made an small adjustment and looks like it's working!
That must be Set<ID>
I always use Sets to store Ids to avoid duplicate IDs. So I have not observed that it was List<Id>
List<Id> leadIds=new List<Id>(); You can Replace this line by the following
Set<Id> leadIds=new Set<Id>();
-Thank you
suggested changes made, and works great! I appreciate your help with above.
- Create custom Lead and custom Contact lookup fields on Task/Event.
- Using Process Builder, when a Task is being created or updated look at the WhoId and if starts with “003” then copy the [Task].WhoId into the [Task].Contact__c lookup field. If “00Q” then copy the [Task].WhoId into the [Task].Lead__c lookup field. Please read this blog post (https://douglascayers.com/2016/06/21/salesforce-how-to-update-lead-or-contact-when-activity-logged) for more specific walkthrough.
- Create formula fields on Task/Event object that expose data from the Lead or Contact lookup fields.
For example, to expose the State field from the Lead or Contact you might have formula field like:If the Lead’s Contact lookup field is populated then the formula will display the field from the contact, else the field from the lead if that lookup is not blank.
No triggers. Just process builder + custom lookup fields + formula fields.
Doug Ayers
douglascayers.com