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
DannyTKDannyTK 

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.
Best Answer chosen by DannyTK
Chidambar ReddyChidambar Reddy
Hi Danny, Sorry for the mistake

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

Ramu_SFDCRamu_SFDC
I do not see any issues with the code either. However, when you say that it does not seem to work i assume that it is not throwing any error but the checkbox on leads does not seem to be updating. This can be due to multiple tasks on the same lead which are switching something on or off (as you did not provide the code under the last FOR loop). Try using SETS rather than LISTS to avoid multiple executions on the same leads.

Please provide the complete code which might help us understand the issue better. Also please let us know if you are getting any error?
Chidambar ReddyChidambar Reddy
Hi Danny

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.
DannyTKDannyTK
thanks guys,

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
Chidambar ReddyChidambar Reddy
Hi Danny, Sorry for the mistake

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.
This was selected as the best answer
DannyTKDannyTK
thanks Chidambar,

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?
DannyTKDannyTK
Thanks for your help Chidambar,
made an small adjustment and looks like it's working!
Chidambar ReddyChidambar Reddy
Hi Danny,
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
DannyTKDannyTK
Thanks Chidambar,

suggested changes made, and works great!  I appreciate your help with above.
Douglas C. AyersDouglas C. Ayers
As of Winter '16 release (https://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_sales_activity_custom_lookups.htm), this is now possible because we can now create custom lookup fields on Task/Event objects (Setup Customize Activities Activity Custom Fields).
  1. Create custom Lead and custom Contact lookup fields on Task/Event.
  2. 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.
  3. 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( ISBLANK( Contact__c ), Lead.State, Contact.MailingState )

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