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
Admin 123Admin 123 

To have the lookup user field as logged in user.

I have this functionality, wherein once I click on new button, it will need to choose specific record type, once I choose, I want my lookup field to be default logged in user?User-added image


How will be the trigger for it?

I have written Apex code as::

trigger Update_Field on my_object (after insert) {
     //user defaultuser = [select id from user where name = 'default user'];
     for (my_objec C:trigger.new) {
        if(C.VTM_Name__c==null) { 
         C.VTM_Name__c= userinfo.getUserId();    
         }
  }
}

but I am still not getting the default value as logged in user.
Best Answer chosen by Admin 123
Maharajan CMaharajan C
Hi Friend,

Try the below it will works for you!!!

It will populate the current User in the Lookup field after save the record.

trigger UpdateUser on my_object (before Insert,before Update) {
Id Id1=UserInfo.getUserId();
     for (my_object C:trigger.new) {
        if(C.VTM_Name__c==null) { 
         C.VTM_Name__c=Id1;    
         }
  }
}

Can you please Let me know if it works or not!!!

If it works don't forget to mark this as a best answer!!!

Thanks,
​Raj