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
ZimmerZimmer 

Task Trigger to Update Date/Time field on Lead

I have written this trigger and it works great to store the first human generated activty date from the task to the lead record.

 

 

trigger ActivityFirstcontact on Task (after insert) {

List<Lead> updatedLeads= new List<Lead>();
 
    for(Task newTask:Trigger.new){
            if(string.valueOf(NewTask.WhoId).startsWith('00Q') && newTask.Subject != 'Lead Source Details')
            updatedLeads.add(new Lead(Id=newTask.WhoId, First_Contact_Date_Time__c=newTask.CreatedDate));
           }
    
              update updatedLeads;
              
   }

 

I am attempting to modify the code to only write to the field 

 

First_Contact_Date_Time__c

when the field is blank.  How can i update the code to manage this. Anytime I attempt to lookup the lead using the whoid i get errors.

 

Thoughts?

Best Answer chosen by Admin (Salesforce Developers) 
ZimmerZimmer

ok i fixed it myself and figured i would update this post for future people.

 

 

trigger ActivityFirstcontact on Task (after insert) {

List<Lead> updatedLeads= new List<Lead>();
 
    for(Task newTask:Trigger.new){
            if(string.valueOf(NewTask.WhoId).startsWith('00Q') && newTask.Subject != 'Lead Source Details')
            {
            Lead changedLead = [select id, First_Contact_Date_Time__c from lead where id=:NewTask.WhoId];
               if(changedLead.First_Contact_Date_Time__c == NULL){
                   changedLead.First_Contact_Date_Time__c=newTask.CreatedDate;
                   updatedLeads.add(changedLead);
           }}
   
              update updatedLeads;
              
   }
}

All Answers

ZimmerZimmer

ok i fixed it myself and figured i would update this post for future people.

 

 

trigger ActivityFirstcontact on Task (after insert) {

List<Lead> updatedLeads= new List<Lead>();
 
    for(Task newTask:Trigger.new){
            if(string.valueOf(NewTask.WhoId).startsWith('00Q') && newTask.Subject != 'Lead Source Details')
            {
            Lead changedLead = [select id, First_Contact_Date_Time__c from lead where id=:NewTask.WhoId];
               if(changedLead.First_Contact_Date_Time__c == NULL){
                   changedLead.First_Contact_Date_Time__c=newTask.CreatedDate;
                   updatedLeads.add(changedLead);
           }}
   
              update updatedLeads;
              
   }
}
This was selected as the best answer
MhaweMhawe

Thanks for posting. I was able to manipulate the variables to solve a similar problem.