+ Start a Discussion

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.