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
JDegnerJDegner 

Update custom lookup field

trigger timecardGetUser on SFDC_Service_Timecard__c (before insert, before update){
    SFDC_Service_Timecard__c a = Trigger.new[0];
    a.User__c = UserInfo.getName(); 
}

 I'm trying to update a custom lookup field with a trigger. I have the code included. When I create a new timecard, the User__c filed isn't being updated! Any ideas?

Phillip SouthernPhillip Southern

Hi, you need to assign it the Id of the User...try UserInfo.getUserId();

 

Keep in mind anytime in apex when you do assignments to lookup relationship, master/detail, objects, etc...Apex will need the record Id, even though on the front end UI salesforce will display the physical name or text.