How to assign value to CreatedById fields via REST API in Salesforce

Ongkrab
3 min readJun 17, 2021

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

  1. We want to define Other user instead of integration user.
  2. Sender will send Employee ID which is a unique the user, to reference user in field CreatedById.

Summary Steps:

  1. Enable ‘Create Audit Fields’ in Salesforce.
  2. Create a Permission set to allow define in audit field.
  3. Testing Call API by Postman.

Steps 1: Enable ‘Create Audit Fields’ in Salesforce

  1. Go to Setup.
  2. Search User Interface in Quick Find box and select User Interface.
  3. 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.
  4. Click Save.
Enable Set Audit Field in Salesforce

Step 2: Create a Permission set to allow define in audit field

  1. Go to Setup,
  2. Search Permission Sets in Quick Find box and select Permission Sets
  3. Click on New Button to create a new permission set and define label and API name or selected the Current permission set
  4. In page permission in step 3, go to Section Permission and select System Permissions
  5. Click Edit button in SETUP Permission sets page.
  6. 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.
  7. Assign user by click button Manage Assigments and add user you allow updated that field
Access System permission in selected Permission Set page
Enabled Set Audit Fields upon Record Creation

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
  1. Authenticate to the server. I am using Username and Password
  2. Step 2 Copy your access_token from return message to set the default token following picture
  3. Create object for testing
  4. In Postman, go to Folder REST and select SObject Create
  5. Define your parameter in Param tab by API name of the Object which you want.
  6. Define your body
Step 1 Authenticate and receive access_token
Step 2 Copy Access_token to set current token
Step 5 Define Object API Name
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"    ]  }]

--

--