Flow is the most powerful automation tool of Salesforce. It contains many great features and there are endless use cases. Moreover, Salesforce keeps improving Flow in every release and it has already become the best friend of the admins. Most of the time, standard features of flow are enough to build the automations that you need. However, in cases that they don't meet your requirements, you can build your own actions or screen components. You can even install components from great sources like UnofficialSF. For instance, you can build actions that make callouts, also known as external API calls. If you already have an action that make a callout, you might be familiar with this error: System.CalloutException: You have uncommitted work pending. If the current transaction has uncommitted work pending, the flow cannot perform a callout. However, there are ways to avoid callout errors.
Commit Transaction to Avoid Callout Errors
In order to avoid these callout errors, you can commit the transaction. Whenever a flow comes to a screen element, pause element, or local action, it commits the transaction. Screen element and local actions are available in screen flows only. Therefore, if it is a screen flow, you can put a local action or a screen element between your callout and any data operations. On the other hand, these elements reset the flow limits too.
There is even a better way to avoid callout errors in screen flows.
Avoid Callout Errors Automatically
Salesforce added an option to control the transaction in screen flows in the Spring '21 release. It is possible to configure actions to let the flow determine whether it requires a new transaction to successfully execute the action.
After adding an action, you will see the Transaction Control options under the advanced section. Note that this section exists just in screen flows. Plus, it is available for all custom actions, and for standard actions only if they perform a callout.
Transaction Control Options
- Let the flow decide: This is the recommended option. The flow determines at run time whether it requires a new transaction to successfully execute the action. If this action involves a callout, and the current transaction has pending operations, it commits the current transaction before executing the action in a new transaction.
- Always start a new transaction: If you select this option, flow commits the current transaction before it executes the action. Then it executes the action in a new transaction. If the action fails for some reason, Salesforce cannot roll back any operations from the previous transaction.
- Always continue in current transaction: If you select this option, flow always executes this action in the current transaction. If the action involves a callout while the current transaction has pending operations, the flow fails.
If the action was configured in Spring '21 or later, the default behavior is "Let the flow decide". However, if the action was configured before, the default behavior is "Always continue the current transaction". Therefore, if the action is making a callout and current transaction has pending operations, flow will fail.
For screen flows, Salesforce recommends letting the flow decide to be the best way to control the transaction. For actions configured in Winter ’21 or earlier, change the transaction control to Let the flow decide (recommended).