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
JayDP123JayDP123 

Setting ownership using CreatedBy field

Hi quick question,

 

does anybody know if it is possible to set the ownership of a record using the CreatedBy field? Because the createdby is not populated until the record is inserted a before insert would not work, I dont think. 

 

Is it commonplace to use an after insert to update the same record being inserted? 

 

Thanks! 

Best Answer chosen by Admin (Salesforce Developers) 
TejTej

You can set OwnerId = UserInfo.getUserId();

 

so it will be the running user who is creating the record(created by).

 

All Answers

TejTej

You can set OwnerId = UserInfo.getUserId();

 

so it will be the running user who is creating the record(created by).

 

This was selected as the best answer
JayDP123JayDP123
Thanks!
klamklam

Can I ask why this is needed? OwnerId is always defaulted to the Id of the creating user.

JayDP123JayDP123

Oh ok, didn't know that either. I am still going to use Tej's recommendation though as I need to check if the current user is of a specific userrole type.