How to Set the Audit Fields Using Flow

How to Set the Audit Fields Using Flow

Audit fields in Salesforce track information about system values that can be useful for audit purposes. Some of the audit fields are CreatedDate, CreatedById, LastModifiedDate, and LastModifiedById. These fields are read only and Salesforce sets the values whenever you create or update a record. For example, when you create a record, Salesforce stores the date and time in the CreatedDate field.

If you migrate records from another system to Salesforce, created date values of all of these records will be the current date. In most of the cases, it is not an acceptable solution. All the records should have their correct created dates.

There is a permission that gives the user to set audit fields upon record creation. However, it is not possible to set these values on create through the user interface.

Enable the Permission to Set Audit Fields Upon Record Creation

Before assigning the permission, you have to enable this feature. In order to enable it, go to the User Interface in setup. Then, mark the checkbox called Enable "Set Audit Fields upon Record Creation" and "Update Records with Inactive Owners" User Permissions.

Enable "Set Audit Fields upon Record Creation" and "Update Records with Inactive Owners" User Permissions

After enabling this feature, you will have the option to add this permission to profiles, or you can create a permission set to assign it to specific users.

Set audit fields upon record creation permission profile.
Set Audit Fields upon Record Creation permission through a permission set

As you can see from the description this permission, you can set audit fields when you create a record through API. So these audit fields will not be editable on the user interface. However, you can also use a flow to set these audit fields. You can expect different behaviors in different flow types.

Build a Screen Flow to Set the Audit Fields on Create

Create a screen flow and add a screen element to the canvas. Let's add a text field for the account name and two date/time fields for the created date and the last modified date.

Flow screen

Add a Create Records element in order to create a new account record. If you assigned the permission to your user, you can map the audit fields, otherwise they won't even show up.

Create an account record

Optionally, handle the errors so that the user can see the error message. Read this post to learn how to use the fault path and handle the exceptions.

Error handling

At the end, your flow should look like this.

End of the flow

Results About the Field Values

  • You cannot select a date from the future.
    FIELD_INTEGRITY_EXCEPTION: Create Date(Thu Jun 30 15:17:00 GMT 2022) in the future.
  • LastModifiedDate cannot be before the CreatedDate.
    FIELD_INTEGRITY_EXCEPTION: Last Modified Date(Mon Jun 27 15:17:00 GMT 2022) before Create Date(Wed Jun 29 15:17:00 GMT 2022) for entity Account.
  • Salesforce accepts dates between Jan 1, 1700 and December 31, 4000. However, the audit fields cannot accept dates before January 1st, 1970.
    FIELD_INTEGRITY_EXCEPTION: Create Date(Wed Dec 31 16:22:00 GMT 1969) too early.
  • If you don't put any value, Salesforce fills it automatically. For example, if you leave the CreatedDate blank, Salesforce sets it as the current date time.

Different Behaviors

Screen flow behaves differently according to the permissions and how you run the flow.

  • If this feature is not enabled in the Salesforce environment, the flow will fail.
  • If you are running the flow in the user context, the user must have this permission. Otherwise the flow will fail.
  • If you are running the flow in the system context, the user doesn't need to have this permission. However, this feature must be enabled in the Salesforce environment.

Record-triggered flows always run in system mode without sharing. Therefore, the running user doesn't need to have this permission. However, this feature must be enabled in the environment. Otherwise, the flow will fail with this message:

INVALID_FIELD_FOR_INSERT_UPDATE: Unable to create/update fields: CreatedDate. Please check the security settings of this field and verify that it is read/write for your profile or permission set.

Pay Attention When Cloning Records

You can get a record or use the $Record global variable and then use the assignment element to change some specific field values. After that, you can use this record variable in order to create a new record. However, if this feature is enabled in the system, it will copy the audit fields as well. So it will create a new record with created date value from the past.

Let's clone the account record when it meets a criteria. Add an assignment element to assign a new account name.

Assign account name

As you can see, this is the only assignment. Let's add a Create Records element and select this variable in order to clone the record.

Clone the account record

If this feature is not enabled in the environment, it will create a new account record with CreatedDate equals to the current date. However, if this feature is enabled, it will set the CreatedDate same as the original CreatedDate. Because it clones all the fields and it can set values for those audit fields too.

Therefore, you have to pay attention to this. Otherwise, you will have new records but they will look like they were created in the past.

Be the first to comment

Leave a Reply

Your email address will not be published.


*