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
Bob BaconBob Bacon 

Trigger to update task field from lead field

I'm just learning triggers and have a relatively simple goal.  When a new task is created I need to copy the contents of a custom field on the Lead object and put it in a custom field on the Task object.

I've been reading here for about 4 hours and the triggers proposed are significantly more complex.  However, based on my reading here is a trigger that I assembled.  Note that lead.type__c is the source field and task.account_type__c is the destination field.

trigger UpdateAccountType on Task (after update) {
    Set<Id> leadIds=new Set<Id>();
    for(Task t : Trigger.new){
    if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){   //check if the task is associated with a lead
        if( !leadIds.contains(t.whoid)){
            leadIds.add(t.whoId);
            t.Account_Type__C = lead.Type__c;
            }}}}

Can anyone help with this or, have I taken a wrong turn and need to start from stratch?

TIA
Best Answer chosen by Bob Bacon
Deepak GulianDeepak Gulian
trigger UpdateAccountType on Task (before update) {
    Set<Id> leadId = new Set<Id>();
    for(Task t : Trigger.new){
        if(t.WhoId != null){
              String s1 = t.WhoId;
               if(s1.left(3) == '00Q'){
                        leadId.add(t.whoId);
               }
         }
    }

    Lead lead = [Select Id, Type__c From Lead Where Id In : leadId limit 1];
    for(Task tk: Trigger.New){
           tk.Account_Type__c = lead.Type__c;
    } 
    
}

 

All Answers

Deepak GulianDeepak Gulian
trigger UpdateAccountType on Task (before update) {
    Set<Id> leadId = new Set<Id>();
    for(Task t : Trigger.new){
        if(t.WhoId != null){
              String s1 = t.WhoId;
               if(s1.left(3) == '00Q'){
                        leadId.add(t.whoId);
               }
         }
    }

    Lead lead = [Select Id, Type__c From Lead Where Id In : leadId limit 1];
    for(Task tk: Trigger.New){
           tk.Account_Type__c = lead.Type__c;
    } 
    
}

 
This was selected as the best answer
Bob BaconBob Bacon
Thanks Deepak!  That trigger worked perfectly!
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