Govur University Logo
--> --> --> -->
...

When would you choose to use a before-save flow versus an after-save flow to automate a specific business process on a record, and what are the implications of each?



Choosing between a before-save flow and an after-save flow in Salesforce depends heavily on the specific business process you need to automate and the timing of the required actions relative to the record's save operation. Each type of flow operates at a distinct point in the transaction lifecycle, leading to different implications for system performance, data consistency, and the user experience.

A before-save flow, also known as a "fast field update" flow, executes *beforethe record is committed to the database. It's triggered when a record is created or updated, and it operates within the same transaction as the record save. This makes it incredibly efficient because it doesn't trigger a separate database transaction. The primary use case for a before-save flow is to modify the record's data *beforeit's saved, effectively changing field values based on conditions or formulas, without any delay. For example, imagine a scenario where a lead needs a 'Lead Source' field automatically populated based on the referring website domain. A before-save flow would be ideal for this because you're modifying a field on the record itself before it is saved. This happens seamlessly without any noticeable lag, resulting in a fast and efficient save. Before-save flows are also excellent for implementing complex validation checks that are more sophisticated than basic validation rules or for concatenating fields, calculating values, or formatting data on the record prior to saving. However, before-save flows have a significant limitation; they cannot access data from other records or execute actions like sending emails, creating related records, or invoking other automation processes, because the data is not yet persisted. Additionally, before-save flows must be handled with care, because if they fail, the transaction will rollback and the user will not be able to save the record, resulting in a poor user experience. Before-save flows should not be used to perform complex operations that could lead to high resource consumption.

An after-save flow, conversely, executes *afterthe record has been committed to the database. It runs in a separate transaction from the record save. This means the original record is fully saved before the flow logic begins. After-save flows are more versatile than before-save flows because they can access other records, perform external calls, send emails, update child records, or invoke more complex automation processes like creating tasks and other related records. For example, when an Opportunity is marked as "Closed Won", an after-save flow could be triggered to create a project record, send notifications to the project management team, and update the account status to 'Customer'. After-save flows are more suitable for actions that require data that is only available after the save operation. Another advantage of after-save flows is that they can trigger other flows or apex code, unlike before-save flows. However, after-save flows have a drawback. Because they operate in a separate transaction, the user may experience a slight delay after saving, as the after-save flow processes. This delay is not directly visible to the user but can affect the system’s performance and may cause governor limits issues if not carefully designed. An after-save flow is not recommended for updating the record on which it was triggered as it may cause infinite loops and could lead to performance issues. Generally, for non-complex actions directly on the triggering record, before-save flows are preferable due to their efficiency, while after-save flows should be used for any process needing access to other records or requiring actions after the initial record save.

In summary, use before-save flows for rapid, efficient updates to the record that is being saved, especially for data formatting and validation, but understand that they cannot execute actions such as email alerts or create other records. Use after-save flows for any actions that need to access other records or require more complex automation, but be mindful of the performance implications, as they run in a separate transaction and are less efficient for in-place record changes. For example, if the requirement is to standardize a lead's phone number before save, a before-save flow would work best. If the requirement is to create an opportunity when a lead is converted and notify the sales team, then an after-save flow is better.