top of page

Salesforce Test Class for Invocable Method – Everything You Need to Know

Salesforce Test Class for Invocable Method

In the fast-growing world of Salesforce development, mastering the complexities of Invocable Methods and their associated test classes is essential. If you're an experienced developer looking to increase your knowledge or a newcomer trying to make sense of it all, our comprehensive guide on “Salesforce Test Class for Invocable Method” has you covered.

This guide is like a passport to the world of Salesforce testing, offering a detailed exploration of Invocable Methods and their critical role in ensuring the integrity of your code.

So, let’s begin!

What Is a Salesforce’s Invocable Method and Invocable Actions?

Invocable Methods are methods defined in Apex classes that you can call from various automation tools within Salesforce, such as Process Builder, Flow, or Apex triggers. These methods can accept input parameters and perform custom logic. They are used to extend the functionality of Salesforce by allowing you to execute your code when meeting certain conditions. Besides, you can use the @InvocableMethod annotation to mark a method as invocable.

On the other hand, Invocable Actions are specific actions that you can use within Process Builder or Flow to execute invocable methods. When you create a process or flow and want to include custom logic, you can add an "invocable action" step and select an invocable method to run. Essentially, invocable actions are a way to use invocable methods in your automation processes.

Overall, invocable methods are custom code snippets you write in Apex, while invocable actions are how you incorporate and utilize these methods within Salesforce's automation tools like Process Builder and Flow. They work together to help you create flexible and customizable automation solutions.

How Invocable Actions Work and What are Their Benefits?

Invocable Actions

How They Work:

  1. Utilizing the InvocableMethod Annotation: Developers designate specific methods within their Apex classes by applying the Invocable Method annotation. It essentially ‘Flags’ those methods that are available as Invocable Actions.

  2. Handling Parameters and Return Types: These methods can receive parameters and return values, enabling flexible operations that depend on provided data.

  3. Use in Flows and Processes: Once a method is marked invocable, administrators can see & utilize it in tools such as Process Builder or Lightning Flow. It appears like a custom action, and you can easily drag or drop it to your desired location in the process or flow.

Benefits of Invocable Actions

Here are the advantages of using Invocable actions:

  1. Empower Non-Coders: Invocable actions empower non-coders to harness custom-built logic without needing to grasp the complexities of the underlying code.

  2. Enhancing Consistency and Efficiency: Instead of developing various solutions for similar issues, developers can create a single, flexible method and make it available for multiple applications, simplifying the development process and ensuring consistency.

  3. Providing Versatility: Invocable actions open the door to executing complicated operations, such as interfacing with external systems or conducting tasks that declarative tools can't handle.

Invocable Method Annotation in Salesforce

Invocable Method Annotation in Salesforce

The Invocable Method annotation helps to label particular methods within Apex classes. When you apply this annotation to a method, you're making it available and usable through Salesforce's declarative tools.

Besides, its primary goal is to enable administrators and individuals with limited coding knowledge to use custom logic and features previously only accessible to developers.

Now, we'll discuss the Syntax and Characteristics of Salesforce's Invocable Method Annotation below:

1. Basic Syntax

public class MyClassName {

@InvocableMethod(label=’Descriptive Label’ description=’Description of the method’)

public static List<ReturnType> methodName(List<ParameterType> parameters) {

// method logic



Note: You can replace the label, description, and other placeholders with the specific data types and method names you are using.

2. Parameters

An invocable method can take just one parameter, which should be in a list format, even if you are passing only one piece of information.

3. Return Types

The method can choose to give back a result if it wants to. Like with parameters, its given result should also be in the form of a list.

How to Write Salesforce Test Class for Invocable Method

Follow the steps below to write the Salesforce test class for the Invocable Method. Here's a basic outline of the process:

1. Create a Test Class

First, create a new Apex test class if you don't have one.

2. Define Test Methods

Inside your test class, define one or more test methods that will test the behavior of your Invocable Method.

3. Create Test Data

If your Invocable Method requires specific data to operate correctly, create that data within your test method. It might include creating records or objects.

4. Invoke the Invocable Method

Now, call the Invocable Method you want to test in your test method.

5. Write Assertions

After invoking the Invocable Method, use assertions to verify that the method behaves as expected. If you find any issues, check the results returned by the method against your expectations.

6. Run the Test

Save your test class and run the test using Salesforce tools (e.g., Salesforce Developer Console or Salesforce CLI).

7. Review Test Results

Lastly, review the test results to ensure your Invocable Method functions correctly and that all assertions pass.

By following these steps, you can create a test class to test the behavior of your Invocable Method in Salesforce.

InvocableMethod Considerations

  1. The invocable method should be global, public, or static and belong to the outer class (not an inner class).

  2. In a class, only one method can contain the ‘InvocableMethod’ annotation.

  3. Triggers can't use Invocable methods.

  4. You can't use any other annotations alongside the InvocableMethod annotation.

  5. You're allowed to have at most one input parameter, and it should be in the form of a list. This list must contain sObject types, primitive data types, generic sObject types, user-defined Apex types, or user-defined types with variables of the supported types.

  6. The method must also return a list.

Sum Up

Writing appropriate test classes for Invocable Methods in Salesforce is crucial for ensuring the reliability and functionality of your custom code. By following best practices and incorporating the steps outlined in this blog, you can confidently test your Invocable Methods, verify behavior, and maintain the integrity of your Salesforce applications.

Check out more of our articles: Test Class Methods in Salesforce.

bottom of page