Salesforce Flow is a powerful tool that allows admins and developers to automate processes without writing code. But sometimes, you need to go beyond what Flow can do out-of-the-box. That’s where Invocable Apex comes in. By creating an Invocable Apex class, you can expose custom functionality to Flow, making it more flexible and powerful.
This blog post only touches on the very basics of leveraging invocable Apex, lets start from the beginning. I find this capability in Salesforce to be one of the most fascinating and powerful tools in my war chest. It offers an incredible amount of flexibility and extensibility.
Why Use Invocable Apex in a Flow?
Flows offer great built-in automation tools, but they have limitations:
- Complex business logic can be cumbersome to build with standard Flow elements.
- Some operations, like handling large data sets, are more efficient with Apex.
- There are actions that Flow simply can’t do natively.
By leveraging Invocable Apex, you can extend Flow’s capabilities while still keeping automation declarative.
Calling Invocable Apex from a Flow
Write the Invocable Apex Class
To expose an Apex method to Flow, you need to use the @InvocableMethod annotation. Here’s an example of a simple Apex class that updates an Account’s rating based on a given value:
public with sharing class AccountRatingUpdater {
@InvocableMethod(label='Update Account Rating' description='Updates the rating of given accounts')
public static void updateAccountRatings(List<AccountWrapper> inputRequests) {
List<Account> accountsToUpdate = new List<Account>();
for (AccountWrapper wrapper : inputRequests) {
Account acc = new Account(Id = wrapper.accountId, Rating = wrapper.rating);
accountsToUpdate.add(acc);
}
if (!accountsToUpdate.isEmpty()) {
update accountsToUpdate;
}
}
public class AccountWrapper {
@InvocableVariable(label='Account Id' required=true)
public String accountId;
@InvocableVariable(label='Rating' required=true)
public String rating;
}
}
Deploy the Apex Class
Ensure that your class is saved and deployed to your Salesforce org. If you’re working in a sandbox, you can deploy it through a change set or use Salesforce DX.
Create a Flow and Add the Apex Action
- Go to Setup → Process Automation → Flows.
- Click New Flow and choose Record-Triggered Flow or Screen Flow, depending on your use case.
- Click the + button to add a new element and select Action.
- Search for your Invocable Apex method (in this case, “Update Account Rating”).
- Map the Flow variables to the Apex parameters:
- Account Id → The account record ID.
- Rating → The rating value you want to assign.
Save, Activate, and Test the Flow
- Click Save and give your Flow a meaningful name.
- Activate the Flow.
- Run a test by providing sample inputs and verifying that the accounts are updated correctly.
Best Practices for Using Invocable Apex in Flow
- Keep It Simple: Your Apex method should do one thing well and be easy to reuse.
- Error Handling: Implement proper error handling (e.g., try-catch blocks) to prevent Flow failures.
- Bulkification: Always process lists of records to ensure your class can handle bulk updates efficiently.
- Testing: Write unit tests to ensure your Apex method works as expected and meets test coverage requirements.
Conclusion
By combining Salesforce Flow with Invocable Apex, you get the best of both worlds: the ease of declarative automation and the power of Apex. Whether you need to perform complex logic, work with large data sets, or interact with external systems, Invocable Apex is a great way to supercharge your Flows.
We will dive much deeper into more advanced invocable actions with Flows and Apex in upcoming blog posts!

We would love to hear your comments!