Imagine you’ve just started as an administrator or developer in Salesforce, and your manager asks you to create automation that updates records in bulk based on certain conditions. You think, “Should I write a trigger? Is there a way to use the declarative tools like Flow Builder?” Enter Invocable Methods—your gateway to bridging complex coding with easy-to-use automation tools like Salesforce Flows and Process Builder.

In this beginner-friendly guide, we’ll demystify Salesforce Invocable Methods, explain their importance, and walk through an example to help you get started.

What Are Invocable Methods?

Invocable Methods are a feature in Salesforce that allow you to write custom Apex logic and expose it to declarative tools such as Flow Builder or Process Builder. In simpler terms, they are like a bridge, connecting the power of coding with the simplicity of click-based tools.

By annotating a method in your Apex class with @InvocableMethod, you make that method accessible for automation builders without requiring them to understand or write code. It’s like giving administrators a magic button to perform complex tasks.

Why Are Invocable Methods Important?

Anecdote: Let’s say a sales manager needs a custom solution to update the priority status of accounts based on their sales figures. Without invocable methods, they’d rely solely on developers to write and manage triggers or batch classes. However, by creating a simple invocable method, developers empower administrators to invoke this logic within their own processes, saving time and fostering collaboration.

Here’s why invocable methods are so valuable:

  1. Seamless Integration: They allow you to use custom Apex code within declarative tools.
  2. Reusability: The same invocable method can be reused across multiple automation flows.
  3. Bulk Processing: They handle multiple records in a single execution, respecting Salesforce’s governor limits.

How Do Invocable Methods Work?

An Invocable Method is a public static method in an Apex class annotated with @InvocableMethod. This annotation tells Salesforce that the method can be called from tools like Flow Builder.

Basic Characteristics of Invocable Methods

  1. Single Input Parameter: They accept a single parameter, which is usually a list of supported types (e.g., primitive data types, sObjects).
  2. Single Method per Class: Each class can have only one method annotated with @InvocableMethod.
  3. Bulk Operations: Designed to process multiple records efficiently.

Example: Updating Account Ratings

Let’s walk through an example where we create an invocable method to update the Rating field of Account records.

Step 1: Write the Apex Class

Here’s a simple Apex class with an invocable method:

public class AccountUpdater {
    @InvocableMethod(label='Update Account Ratings' description='Updates the Rating field for a list of Accounts')
    public static void updateRatings(List<Id> accountIds) {
        List<Account> accountsToUpdate = [SELECT Id, Rating FROM Account WHERE Id IN :accountIds];
        for (Account acc : accountsToUpdate) {
            acc.Rating = 'Hot'; // Example update
        }
        update accountsToUpdate;
    }
}

Explanation:

  • The @InvocableMethod annotation makes the method callable from flows.
  • The method accepts a list of Id (account IDs) and updates the Rating field to “Hot.”

Step 2: Use It in Flow Builder

  1. Go to Setup > Flow Builder.
  2. Create a new flow and select Record-Triggered Flow or Autolaunched Flow.
  3. Add an Action element and search for the invocable method (e.g., “Update Account Ratings”).
  4. Pass the account IDs as input to the method and configure your flow.

Best Practices for Invocable Methods

  1. Follow Single Responsibility Principle: Ensure each method performs only one task.
  2. Handle Errors Gracefully: Include error handling mechanisms to manage exceptions.
  3. Document Your Method: Use descriptive labels and comments to explain the method’s purpose.
  4. Test Before Deploying: Always test invocable methods thoroughly in a sandbox.