#BeforeSave

2024-12-02

Salesforce Flow Best Practices

Best practices in flow revolve around principles that enhance low-code quality, maintainability, and collaboration. Key practices include, but are not limited to, building clear and concise automation with meaningful resource names to improve readability and consistently adhering to standards for uniformity.

Overall, following best practices leads to more efficient and effective automation.

Antipatterns in flow refer to common practices or solutions that may seem beneficial but ultimately lead to poor software design, increased complexity, or maintenance challenges. They are the opposite of best practices and often arise from attempts to solve problems without fully understanding the implications. Recognizing and avoiding antipatterns is crucial for creating clean, efficient, and maintainable code.

While reviewing legacy flow automation is very different from reading and reviewing code, the visual builder flow canvas presents opportunities to spot antipatterns easily.

I gathered the antipatterns I have seen throughout the years and created a slide-set for a comprehensive list. This list is not meant to be the definitive list. I hope it will start fruitful discussions around the topic, which may yield better flow automation quality in the ecosystem.

This post will start a best-practice blog post series. Each item on the list will receive a comprehensive post, and I will link it to this master post upon completion.

Without further ado, let’s dive into the list:

  1. Can You After-Save When You Can Before-Save? Consider performing the same record updates in before-save flows.
  2. Can You Use DML or SOQL Inside the Loop? While there are situations where you may need to break the rule, DMLs and SOQLs (Create, Update, Delete, and Get) should be placed outside the loop.
  3. Is Your Salesforce Flow Too Big? Consider a modular approach and leverage subflows for an easily maintainable structure when your flow becomes too big.
  4. Can You Start With a Loop Inside Your Schedule-Triggered Flow? When you loop immediately after a start element with an object associated, you will loop for every record the flow launches on. This design is not often intended and will potentially generate faults.
  5. Can You Loop Inside a Loop? While this may be necessary in certain situations, it is often a sign of a weak design. Loops inside loops will cause a high number of iterations, hurting performance and usually yielding faults.
  6. Can You Start With a Decision Inside Your Record-Triggered Flow? Starting your record-triggered flow with a decision creates two or more distinct paths inside your automation, which you can split into multiple flows with tighter start element conditions. The latter often is the better-performing design.
  7. Formula Resources in Criteria Conditions—Yes or No? Instead of inserting formula resources in criteria for decisions and updates, you should consider building multi-line conditions combined with AND and OR operators.
  8. Can Your DMLs Have Criteria Conditions Other Than Id? Updates that include conditions beyond specifying the Id of the record consume one SOQL and one DML against your execution governor limits; ensure your condition is necessary.
  9. Can You Leave Unused Input and Output Variables? Mark your variables as available for input and output only when necessary for security reasons.
  10. Is Your System Context Usage Necessary? Heavy-handed use of system context without sharing can cause unwanted changes in your database. This increases the harm caused by a potential attacker running the flow.
  11. Should You Use Fault Paths? Your DMLs, SOQLs, and Actions need a fault path to ensure the whole transaction does not fail. Fault paths can offer custom messages and alternative routes, preventing complex system fault messages from being displayed to the user.
  12. Should You Use Roll Back Records In Screen Flows? Roll Back Record element provides a way for you to roll back the recent DMLs,  and ensures that half-baked record updates don’t persist in the database.
  13. Is Hardcoding Ids a Good Idea? Hardcoded Ids are potential points of failure especially when deploying flows between unconnected environments like scratch orgs.
  14. What Is The Best Way Of Checking Record Types in Record-Triggered Flows? You can check for record type developer name or name in your record-triggered flow start element without needing a get element inside your flow.
  15. Should You Hardcode Business Logic In Your Flow? If there is frequently changing business logic in your flow, the preferable approach is to build this on a table outside your flow and have SMEs update the reference table.
  16. Are Your HTTP Callouts and Outbound Messages on an Async Path? Salesforce requires integration messaging to execute on an async path. In schedule-triggered flows, the wait element provides a similar function.
  17. Is Your Flow Comparing and Processing Collections? When you go down into the related records, find junction objects, and perform enhanced operations with collections, you may be better off leveraging Apex.
  18. Are Your Email Actions on an Async Path? Perform email actions and create trivial object records for internal follow-up on an async path. This ensures that the main part of the execution does not slow down or stop due to performance or error reasons.
  19. Other Miscellaneous Items: Testing, naming, comments, documentation, null check etc.

Let’s start with the obvious: Build in the sandbox, debug, and test your flows very well. Create good documentation for your flows, add comments and descriptions, and follow naming conventions.

Here are the slides I created for this topic:

View and download Salesforce Flow Antipatterns and Best Practices here.

I presented these items at London’s Calling, Apex Hours, and Trailblazer User Group Meetings.

Everyone is welcome to comment on the blog posts, and I will read them and respond to them.

Explore related content:

6 Things You Can Do With The Transform Element

Top 9 Salesforce Winter 25 Flow Features

#AfterSave #Antipatterns #BeforeSave #Element #HowTo #Loop #Record #RecordTriggered #Update

This is the Flow Best Practices blog image
2024-11-04

Keep Salesforce Data Clean With Before Save Flows

Data quality is essential for every Salesforce administrator, as it influences reporting accuracy, decision-making, and overall user confidence. With users constantly inputting data through multiple interfaces, maintaining clean, standardized field values is an ongoing challenge. Fortunately, Salesforce provides tools like validations, reports, batch updates, workflow rules, and processes to help manage data consistency. The introduction of before-save flows in the Spring ’20 Release has transformed data handling, allowing admins to update field values as records are created or modified—without needing complex code. This guide will explore how before-save flows can help streamline data entry, reduce errors, and ensure clean, accurate field values across all data channels.

There are several tools available in Salesforce to ensure clean data:

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

Salesforce administrators have the functionality to update field values by triggering a before-save flow when a record is created and/or updated. Admins do not need to write code to start the update.

Understanding The Before-Save Flow Execution

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 saved, 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.

Advantages of Before-Save Flow Updates

Before-save updates are executed before the data is saved; 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 on Salesforce. You may also have external users entering data on a flow that runs on a digital experience (community) site. If you use field validation rules, you will need to set them up separately on all 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.

Examples of Before-Save Flow Applications

🚨 Use Cases 👉🏼 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 Phone Entry

I built a solution for volunteers to check-in on a digital experience site using their mobile phone number. One potential issue 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

You can easily write a SUBSTITUTE formula to strip the special characters and spaces out of these entries.

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

The Flow

Start a record-triggered before save flow.

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 on the screen and assigning a value to what will be written to the database.

Please note that I recommend using the update element before save instead of the assignment element currently, although the functionality is the same.

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 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 finds the ()- and space characters in a string and removes them.

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.

Enjoy.

Explore related content:

Popular Validation Rules for Salesforce Flows including Phone, Email and Address Fields

6 Things You Can Do With The Transform Element

Create by Checking a Matching Record in Flow

#Administrator #Applications #Automation #BeforeSave #Developer #FieldValues #Formula #HowTo #Salesforce #SalesforceTutorials #UseCases

This is the Clean Field Values Blog Image

Client Info

Server: https://mastodon.social
Version: 2025.04
Repository: https://github.com/cyevgeniy/lmst