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
Melissa Parker 17Melissa Parker 17 

Update lookup field with login user

Hi All,

Is it possible to update a lookup field to the User object of the person who is creating that record? We have a custom object with a Lookup field (to user) called "Sales Rep". We would like to 'default' this field to the person who is creating that record at that time (current logged in user).

The owner field will be changed to someone else in the meantime. Thanks for the help!
R Z KhanR Z Khan
You can create a trigger that would run on insert. Apex will return the current user's Id using userInfo.getUserId(). Assign that Id to the lookup field of the records created
Melissa Parker 17Melissa Parker 17
Thank you. Is there any way for you or anyone to help me out with creating this code? I'm not very skilled with Apex Coding.
R Z KhanR Z Khan
replace "ChildObject__c" with the nameof your object. And .User with the API name of your "Sales Rep" field if its not called User.
trigger NameOfTrigger on ChildObject__c (before insert){
  for(ChildObject__c obj : trigger.new){
       obj.User = userInfo.getUserId();
}
}
Melissa Parker 7Melissa Parker 7
Thank you, RZ! Your the best!
 
R Z KhanR Z Khan
Your welcome Melissa. Please mark this question as resolved if you dont have any more questions regarding the trigger. 
Morgan Henris 10Morgan Henris 10
I realize this is an old one but just in case anybody still needs this, it can be done using process builder.

Choose field update as the action, and update the field using a formula.  The contents of the formula should be $User.Id

This will populate the user lookup field with the logged in user.