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
Mark E 3Mark E 3 

Update USER fields from TASK fields on TASK Create

I am spending too much time on this - and I thought this would be REALLY easy.

GOAL:  Update USER custom fields with custom fields from TASK on TASK CREATE.  Here's what I tried - it compiles, but does not work:

trigger UpdateLastChkInOnUser on Task (after insert) {
for(Task t: Trigger.new)
{
   User u = [Select Id from User where Id = :t.whoId] ;
   u.LastChkIn_DateTime__c = t.Check_In_Date_Time__c ;
   u.LastChkIn_Latitude__c = t.Check_In_Latitude__c ; 
   u.LastChkIn_Longtitude__c = t.Check_In_Longtitude__c ;
   update u;
}

}
Best Answer chosen by Mark E 3
sushant sussushant sus
Hi mark ,

I think their is a gap in understanding ,in task who id can be contact or lead , so it can't be user so trigger you have written wont work .
t.whoId will be contactid or lead id 

please like if its help 

All Answers

sushant sussushant sus
Hi mark ,

I think their is a gap in understanding ,in task who id can be contact or lead , so it can't be user so trigger you have written wont work .
t.whoId will be contactid or lead id 

please like if its help 
This was selected as the best answer
Mark E 3Mark E 3
Yup  - That worked.  changed to "t.ownerid" - no it works great - thanks!
sushant sussushant sus
its great that it works .
But code written above is not up to  best practices as it is not bulkify . so make changes . Thanks 
please like if its help 
Mark E 3Mark E 3
Yes - here's final:

trigger UpdateLastChkInOnUser on Task (after insert) {
List<User> userList = new List<User>();
for(Task t: Trigger.new)
{
User u = new User();
   u.Id = t.OwnerId;
   u.LastChkIn_DateTime__c = t.Check_In_Date_Time__c;
   u.LastChkIn_Latitude__c = t.Check_In_Latitude__c; 
   u.LastChkIn_Longtitude__c = t.Check_In_Longtitude__c;
   userList.add(u);
}
update userList;
}