Automation, Flow

Keep Salesforce Field Values Clean with Before-Save Updates in Flows

Ensuring data quality is a challenge for every Salesforce administrator. Users enter new information into the system using multiple interfaces every day. There are several tools available to keep the data clean in Salesforce:

  • Validations
  • Reports
  • Batch updates
  • Workflow rules
  • Process
  • Code

Up until recently, flow could not be used by itself for this purpose. With Spring 20 Release, Salesforce administrators now have the functionality to update field values by triggering a before-save flow when a record is created and/or updated. Admins no longer need to use a process or write code to start the update.

Before we go into the details of the use case and the solution, let’s dissect and understand what this all means. When a user creates or updates a record there are several steps that are executed while the data is being written to the database. When we use popular automation tools and trigger an update on the fields, what typically happens is that the data the user provides is written to the database, and then overwritten based on the automation rules that are implemented in the system. This update method is not very fast, and presents several potential complications: Duplicate rules can kick in based on the data that is initially entered into the system and produce unexpected results, although the final data saved based on the active automation rules may not trigger the same duplicate rule.

Before-save updates are executed before the data is written to the database; very early in the order of execution. This makes them very fast, and also more predictable because there is a lower chance that they produce unexpected results interacting with other automation rules.

Another advantage of before-save updates is that they are executed regardless of what channel the update is coming from. You may have internal users entering data in Salesforce. You may also have external users entering data on a flow that runs on a community. If you use field validation rules, you will need to set them up separately on all entry channels. When you set up a before-save flow update, on the other hand, it will be executed whenever the data is entered and/or updated regardless of the channel.

What is this solution good for:

  • Clean special characters from phone field entry on the contact
  • Update custom record name combining several field values on the same object
  • Update accounts based on employee count and categorize them by Small/Medium/Large

Main limitations:

  • The flow operations that can be performed are limited (please see the image).
  • Before-save flows can not perform create or update operations on the related objects and their fields.

Clean Special Characters From a Phone Field Entry

This blog post titled “BEAM: Volunteer Hours Management Solution (Check-in/Check-out)” describes a flow solution that searches the contacts in the database using their mobile phones and gets their information to perform several operations on their related objects. One problem with this flow is that the users enter phone numbers into the system in various formats. For example, these entries are very common:

  • 5555555555
  • 555 555 55 55
  • (555) 555-5555

It is not that hard to write a SUBSTITUTE formula to strip the special characters and spaces out of these entries.

This means that we can build a very simple formula to take the phone data entry value, and replace it with a new value before it is written to the database. The new value will be in the format of the first bullet above.

The Flow

It is important to note that when creating this flow the autolaunched flow type needs to be chosen.

The flow consists of two elements only: The start element and one assignment element. Please remember that there is no need for a get or update element. We are working with the value that is entered into the screen field and assigning a value to what will be written to the database.

The flow is short and simple.

The first step on our flow will be to adjust the settings on the start element. When the start element is double-clicked, the flow editor displays several settings that can be adjusted. In this example, we would like to launch our flow when a contact record is created or updated to make fast field updates. The correct settings are shown in the image below.

Choose and save the correct settings for the flow on the start element.

The second element in the flow is a simple assignment element. {!Record.MobilePhone} refers to the mobile phone field on the contact record that the user is about to create or update. This step assigns a formula value to the mobile phone field.

The Formula

Here is the {!CleanMobilePhone} formula:

SUBSTITUTE(
SUBSTITUTE(
SUBSTITUTE(
SUBSTITUTE({!$Record.MobilePhone} , "(", ""), ")", ""), " ", ""), "-", "")

It is a simple formula that says find the ()- and space characters in the string and remove them.

The formula calculates the new mobile phone string.

The Result

When this flow is saved and activated, it updates all mobile phone entries before they are saved to the database. The field value will only consist of digits. Please remember that Salesforce formats the phone fields for display purposes on the screen. You will see (555) 555-5555 displayed on the screen, but when you go to edit the field, you will find out that the value only includes digits.

Since before-save flows finish their work before Salesforce displays the record after a create/update step, there is no need to refresh the screen to see the updated value.

With Summer 20 Release, the flow types and the start element are changing. Please see this video on how this flow is built in the new Release.

Enjoy.

Please post your questions below.

Leave a Reply