10 Salesforce Flow Tips and Best Practices

10 Salesforce Flow Tips and Best Practices

Flow is the most powerful automation tool of Salesforce. Following the retirement of Process Builder and Workflow Rules, it's crucial for every Salesforce admin to learn Salesforce Flow. Yet, with great power comes great responsibility. Since it is a very powerful tool, one must be careful and follow the best practices when building flows. Here are 10 Salesforce Flow tips and best practices.

1- Avoid Performing DML Operations or SOQL Queries in a Loop

This is one of the most important tips. It is more than a best practice and you can consider this as a rule.

Since Salesforce is a multi-tenant environment, there are some governor limits that apply to code and automation. For instance, total number of DML statements issued per transaction is 150. On the other hand, you can have up to 100 SOQL queries in a single transaction. Therefore, if you perform DML operations or SOQL queries in a loop, you may hit the governor limits.

In order to avoid hitting the governor limits, you should always perform the DML operations (edit, create, or delete) and SOQL queries (get) out of your loop. In order to do so, use an assignment element inside the loop. At the end of the loop, you can perform the actions for all the records at once.

Flow Best Practices: Avoid performing DML operations or SOQL queries in a loop.

Read this post to learn how to work with collections in flow.

2- Avoid Hardcoding Ids

While building flows, sometimes you may need to reference record Ids of users, record types, permission sets, etc. Although the fastest way is to use hardcoded Ids, it is not a best practice at all. Because the Id that you hard code in your flow may not be valid in other environments. Therefore, when you deploy your flow to another environment, you may need to edit the flow and change the Id value.

Instead of hardcoding Ids, you should use a value that is common in all of your environments. For instance, instead of hardcoding the record type Id, you should use the developer name of the record type.

If it is a record-triggered flow that you want to run for a specific record type, you can write a formula and check the record type's developer name.

Avoid Hardcoding Ids

Certain situations require using record Ids. In such cases, use a Get Records element to retrieve the necessary Ids.

Get Record Type

3- Use Subflows for Repeated Logic

In some situations, there can be multiple flows that use the same set of actions. Instead of building the same logic over and over again, one of the best practices is to use subflows. Therefore, you can build the logic once (as an autolaunched or screen flow) and call it as a subflow whenever you need to perform those actions.

Using subflows reduces the complexity of your flows, makes it easier to maintain the logic, and saves time.

Here is a flow that creates a contact record. Before creating the contact, it calls an existing flow that validates the values.

Flow Best Practices: Use Subflows for Repeated Logic

4- Use Fault Paths to Handle Errors

Flows, no matter how well they are built, can encounter failures. Even if you follow the best practices, there can be many reasons for failure. Various factors such as record locking, governor limits, bad data, or new validation rules can be the reason for the failure.

If you don't use fault paths in your flows, users may get a red error message that doesn't really explain the reason.

Flow Error

In order to let the users know what went wrong and take actions, it is a best practice to use fault paths.

Use Fault Paths to Handle Errors

Salesforce introduced the Custom Error element in the Winter '24 release. Using this element, you can display custom error messages in record-triggered flows as well.

Custom Error Element in Fault Path

Here is what the custom error messages look like.

Display Custom Error Message

5- Choose the Correct Trigger Type for Record-Triggered Flows

Depending on what you want to perform, you can choose the record-triggered flow to run before or after the record is saved. In the flow UI, "Fast Field Updates" option means before save and "Actions and Related Records" means after save.

The only thing that you can perform in before save record-triggered flow is to update the triggering record. However, before save flow runs much faster (10 times) than the after save flow. Moreover, it doesn't consume from the DML limit.

It is essential to know the differences between before save and after save record-triggered flows. If you just need to update the triggering record, go with before save!

Before Save and After Save

6- Set the Trigger Order of Record-Triggered Flows

It is clear that before save flows run before the after save flows. However, when you have multiple flows with the same trigger type and start criteria, or flows without any start criteria, determining the execution order becomes a question.

Salesforce's Spring '22 release unveiled the Trigger Order feature for record-triggered flows. This feature allows you to specify the trigger order of record-triggered flows for a particular object. When you enter a value to trigger order, flows with the same trigger type on the same object will run in ascending order. It is crucial if there are flows that depend on each other.

Best Practices - Set Flow Trigger Order

7- Flow is a Very Powerful Tool: Be Careful

Unlike the other automation tools like Process Builder and Workflow Rules, Flow is a very powerful tool. There is almost nothing that you cannot do using flows. Moreover, it has a user friendly UI that makes it easy to build automations. However, it also means that it is very easy to mess things up. For instance, if you put a wrong criteria for the Delete Records element, you can delete all of your data.

On the other hand, you can even allow non-admin users to perform admin tasks. For instance, using an Apex action, one can even change other users' passwords. If you are building critical and risky flows like this, make sure that access is restricted to authorized users.

For screen flows, It is possible to restrict access to enabled profiles or permission sets.

Restrict Flow Access

There are other ways to give access to specific users too. For instance, you can check if a user has a specific permission set assigned, use custom settings, custom metadata, or any criteria that you want.

Decision Element to Check Permissions

8- Stay Updated and Use New Features

Flow is a very popular tool right now and Salesforce is adding tons of new features in every release. Make sure that you stay updated, as things change frequently.

For instance, there is a very popular workaround to build complex validations using before save record-triggered flows. The idea is to mark a checkbox and fire a validation rule to trigger an error message. However, Salesforce introduced the Custom Error element in the Winter '24 release. Using this new element, there is no need to create a checkbox field and a validation rule for each error message that you want to display. You can just use this element to display a custom error message and that's it.

On the other hand, now it is possible to use flow to prevent record deletion. It is a new feature from the Winter '24 release. Without knowing this recent update, one might assume that the only alternative is Apex code. Therefore, staying updated and using new features save a lot of time, effort, and money.

9- Don't Give Up If a Standard Solution is Not Available

While you can accomplish nearly anything with the standard flow elements, sometimes they may not be enough. In such situations, don't give up. You can install or build your own Apex actions and screen elements. Numerous valuable flow packages are available from resources like UnofficialSF or Salesforce Time.

Flow Best Practices: Use Custom Components

10- Embrace Creativity and Think Outside the Box

Flow is beyond an automation tool and it is possible to use flows for many use cases. For instance, you can build a screen flow that displays a toast message or summarized data like a report. It is even possible to build invisible screen flows that take action whenever someone views a page.

Flow offers limitless possibilities beyond data modification. You just have to embrace creativity and think outside the box! For instance, here is an invisible screen flow that displays the related task count as a toast message whenever someone views an account.

Bonus: Document Your Flows

Documenting flows is crucial due to their potential complexity. After a certain point, it's nearly impossible for other admins to understand all steps. Documentation stands as one of the most vital flow best practices.

Additionally, write descriptions for flow elements to facilitate admins' understanding of the process. As you may know, it is possible to view the element descriptions in auto-layout mode.

Document Your Flows

Be the first to comment

Leave a Reply

Your email address will not be published.