THE POWER OF USING BEFORE-SAVE UPDATES IN FLOWS

You are currently viewing THE POWER OF USING BEFORE-SAVE UPDATES IN FLOWS

As Salesforce admins and developers, we – on a daily basis – strive to keep our Salesforce orgs lightning fast to provide a smooth and delightful experience to the users. Although, most of the times there is more than one way to fulfil a requirement in Salesforce, we ought to do things the right way. Salesforce being aware of our pain points and needs related to performance around Flow components, introduced Before-Save updates to Flows in its Spring ’20 release. This means that instead of using the process builder to make additional updates when a record is changed, we can now use record triggers for flows to make before-save updates and get things done up to 10 times faster.

Before-save updates made by a flow are similar to before (apex) triggers. However, one thing to note is that the before save-updates are executed just prior to the Apex Before-Triggers.

In order to understand and learn how to use this new gem, let’s go through the following example – 

Use Case:

Wayne Enterprises is a sales organization with multiple showrooms/branches across the globe that uses a ranking mechanism for its sales representatives to determine which sales rep is a best match for an opportunity based on certain parameters. They are smart and savvy and use Salesforce as their CRM. John Smith, a talented System Administrator at Wayne Enterprises, has been tasked with making sure that a sales rep’s ranking under the same branch is never duplicated. This means that 2 sales reps under the same branch should always have a unique rank and if a Sales Manager tries to assign the same rank to another rep, he/she should get an elegant error message on the screen.

Solution Overview:

The flow enables us to update values (before-save) for a particular field in the record being edited. 

  1. Flow checks whether another record with the same rank exists within the same branch
  2. If such a record exists, the flow assigns the value “Disapproved” to a field named “Rank Validity Flag”
  3. This, in turn, triggers a validation rule and an error appears on user’s screen

Let’s set it up in a step by step approach.

Note – 

  • Contact object is used for storing sales representative records
  • Account object is used for storing branch information

Step 1: Create a picklist field named “Rank Validity Flag” in the Contact object as shown below. This field can contain two values – “Approved” and “Disapproved”.

Step 2: Create a validation rule named “Rank_Validity_Flag_cannot_be Disapproved” with the error condition formula as “ISPICKVAL(Rank_Validity_Flag__c, “Disappoved”)”. Put in an appropriate error message. In this case, the error message reads “Cannot assign this rank as it has already been assigned to another sales representative under the same branch.

Step 3: Go to Setup > Flows > New to create a new flow.

We want to create an Autolaunched Flow. So let’s select that and click Create.

Now, double click on Start. We want to create a flow that updates a contact record whenever it’s created or updated. Let’s select the options as shown below and click Done.

Step 4: Drag and drop a Get Records element. Here, we’re finding all other contact records that have the same AccountId and Rank as the record being edited. 

Filter conditions to get records meeting our criteria are shown below.

To store the information of the record(s) fetched, we don’t need to create new resources. This is done by salesforce itself and the values of all fields of the fetched record(s) shall be accessible in the next step. 

Here, we’re only storing the first record as we want to trigger the validation rule even if there’s one other record with the same Rank and AccountId.

Step 5: Drag and drop a new Decision element. This element controls the flow depending on whether there’s a conflict in rank or not.

Let’s set an outcome named “Yes, another record found” which is executed when some other record with the same AccountId and Rank is found.

Step 6: Drag and drop an Assignment element in the canvas. Its purpose is to set the “Rank Validity Flag” to “Disapproved” which in turn, shall trigger the validation rule that we created in step 2.

That’s it! All we need to do now is connect the elements as shown below and save & activate the flow.

Our process is ready in 6 easy steps. Let’ check out how this process performs in the org. There are two sales reps Dick Grayson and Barbara Gordon working under the same branch ‘Gotham Corp Branch’. Presently, Dick’s and Barbara’s ranks are 1 and 2 respectively. So, let’s see what happens when their manager Ethan Bennett tries to change Dick’s rank to 2.

Voila! Our process works like a charm and is incredibly faster and way better performing than how it would have been with the process builder. Ethan (Sales Manager) will be grateful to John (System Admin) forever for delivering such a cool functionality in a  matter of hours and making his life so much easier .