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
Shreekant SahuShreekant Sahu 

Set value for Created By field of an object in Salesforce

In salesforce I have a requirement that, I need to set the Created By field value to a particular user, irrespective of the user who creates it through UI/Apex. So how can I do that?
Best Answer chosen by Shreekant Sahu
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Shreekant,
You can try this one -

1) Create a prermission set.
2) Go to Created permission set -> System Permission.
3) Look for "Set Audit Fields upon Record Creation" and make it true.
4) Assisgn that 'permission set' to the login user.
5) Now try to create a recrod. It should work.
Contact c = new Contact();
c.lastName = 'test ccccccccc';
// Give the Id of the user, you want to show in createdBy field.
c.createdById = '00528000002B6Tsdssadasdsdsdc';
insert c;
Hope this will help you.

Thanks,
Sumit Kuamr Singh

All Answers

Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Shreekant,
You can try this one -

1) Create a prermission set.
2) Go to Created permission set -> System Permission.
3) Look for "Set Audit Fields upon Record Creation" and make it true.
4) Assisgn that 'permission set' to the login user.
5) Now try to create a recrod. It should work.
Contact c = new Contact();
c.lastName = 'test ccccccccc';
// Give the Id of the user, you want to show in createdBy field.
c.createdById = '00528000002B6Tsdssadasdsdsdc';
insert c;
Hope this will help you.

Thanks,
Sumit Kuamr Singh
This was selected as the best answer
Shreekant SahuShreekant Sahu

Hello Sumit,

So here - to get the above functionality always we need the help of apex code, right?

We can not do anything for the records created through UI.

 

Thanks,

Shreekant Sahu

Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Shreekant, 
You can create through UI also. If the page is standard page follow this steps-

1) Create a lookup "Created by User" field to 'User' on the object.
2) Create permission set and assigned to the user (As mentioned in the previous comment.)
3) Create a section on the standard page, and  show the custom "Created by user" field on the edit layout. Hise this on detail page layout. On detail page layout show the standard 'created by' field.
4) Create a work flow rule/ process/trigger to copy the value from custom "Crated by User" field to standard "Created by" field.

Hope this will help you. If you still need help ping me on skype "sumit3887"


Thanks, 
Sumit Kumar Singh