How to assign value to CreatedById fields via REST API in Salesforce
If you have a requirement for defining value Created By, Created Date, Last Modified By ID, Last Modified Date via API call, I will explain the step to allow Salesforce edited that field.
Need to know
This solution will allow only on Salesforce API 51.
- We can define the fields only API Call.
- These fields can definitively an only INSERT step.
- This definition should be in dedicated user.
Requirement
- We want to define Other user instead of integration user.
- Sender will send Employee ID which is a unique the user, to reference user in field CreatedById.
Summary Steps:
- Enable ‘Create Audit Fields’ in Salesforce.
- Create a Permission set to allow define in audit field.
- Testing Call API by Postman.
Steps 1: Enable ‘Create Audit Fields’ in Salesforce
- Go to Setup.
- Search User Interface in Quick Find box and select User Interface.
- Ctrl+F/Command +F with keyword ‘Audit’ to
Select the checkbox for Enable “Set Audit Fields upon Record Creation” and “Update Records with Inactive Owners” User Permissions. - Click Save.
—
Step 2: Create a Permission set to allow define in audit field
- Go to Setup,
- Search Permission Sets in Quick Find box and select Permission Sets
- Click on New Button to create a new permission set and define label and API name or selected the Current permission set
- In page permission in step 3, go to Section Permission and select System Permissions
- Click Edit button in SETUP Permission sets page.
- Ctrl+F/Command +F with keyword ‘Audit’ to Select the checkbox for Enable “Set Audit Fields upon Record Creation” User Permissions. *This row will not show if you do not enable in Step 1.
- Assign user by click button Manage Assigments and add user you allow updated that field
—
Step 3: Testing Call API by Postman
Prerequisite:
- Install Postman on your desktop or your website. Download
- Fork Salesforce API to your postman. You can follow the link.
- Created Connected app to allow the sender integrate with your Salesforce
- Authenticate to the server. I am using Username and Password
- Step 2 Copy your access_token from return message to set the default token following picture
- Create object for testing
- In Postman, go to Folder REST and select SObject Create
- Define your parameter in Param tab by API name of the Object which you want.
- Define your body
Body Message for POST {{_endpoint}}/services/data/v{{version}}/sobjects/:SOBJECT_API_NAME{ "Name": "test 1", "Account__r": { "Account_Number__c": "88888" }, "CreatedBy": { "EmployeeID__c": "12314" }}
- CreatedBy field, you can change to CreatedById if you know the User Record Id from external
- Account__r field, you can change to Account__c if you know the Record Id
- The Other Fields: CreatedByIdDate, LastModifiedBy, LastModifiedDate
The Error Message you might find:
- Message: Unable to create/update fields: CreatedById. Please check the security settings of this field and verify that it is read/write for your profile or permission set. You should check on your Permission set which have already enabled Audit Fields or not.
[ { "message": "Unable to create/update fields: CreatedById. Please check the security settings of this field and verify that it is read/write for your profile or permission set.", "errorCode": "INVALID_FIELD_FOR_INSERT_UPDATE", "fields": [ "CreatedById" ] }]