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
nisha c onisha c o 

CreatedBy ID field of User object

Hi,

 

i am writing a formula field on User object which access CreatedBy ID field. but does not seem that i can access that field. it does not appear when i click on Insert Field button. does any one know how to access that field or any other work around?

 

thanks in advance!

 

Best Answer chosen by Admin (Salesforce Developers) 
Tejpal KumawatTejpal Kumawat

Hi Nisha,

 

Firstly create this field on User Object : Created_By_Id__c [Text(18)]

 

After that paste this one code for User Object :

 

trigger updateCreatedById on User (before insert, before update) {
if (Trigger.isBefore) {
if (Trigger.isInsert) {
for(User u : trigger.New){
u.Created_By_Id__c = UserInfo.getUserId();
}
}
}
}

 

Now test it by creating new User.

 

All Answers

Tejpal KumawatTejpal Kumawat

Hi Nisha,

 

Write a trigger on insert of User Object and Create a Text field on User Object. After that you can access that field as per your requirement.

Tejpal KumawatTejpal Kumawat

Hi Nisha,

 

Firstly create this field on User Object : Created_By_Id__c [Text(18)]

 

After that paste this one code for User Object :

 

trigger updateCreatedById on User (before insert, before update) {
if (Trigger.isBefore) {
if (Trigger.isInsert) {
for(User u : trigger.New){
u.Created_By_Id__c = UserInfo.getUserId();
}
}
}
}

 

Now test it by creating new User.

 

This was selected as the best answer
nisha c onisha c o

thank you so much!! it worked! :)