Invocable Apex Functions

Outline

Salesforce Invocable Apex Functions give you the power to combine Apex and Flows. Once built, you will be able to call your Invocable Function directly from a Flow allowing you to mitigate several limitations of the Flow architecture (such as 2000 operations/transactions, making HTTP callouts, running SOQL, etc...) and bring the trigger logic out of Apex allowing for easy updates!

Getting Started

What we will be doing

This article will walk you through setting up your very first Invocable Apex Function from start to finish. For more references on Apex check out the Official Docs and Tutorials Point Apex walkthrough.

Creating the Apex Class

The Invocable Apex Function is simply an Apex class with a few fancy wrappers. Our main input/output function gets the @InvocableMethod(<description>) wrapper applied. This tells Salesforce to treat this function/method as an Invocable Function. Up next we have the input/output wrappers. These get the @InvocableVariable wrapper applied which allows you to interact with them directly from your Flow. These can be a variety of data types, from numbers, to strings, and even lists of records allowing for great flexibility.

public with sharing class InokableApexExample {
    @InvocableMethod(label='Example to demonstrate the power of Invocable Apex Functions')
    public static List<FlowOutputs> getInokableApexResults(List<FlowInputs> requests){
        List<FlowOutputs> results = new List<FlowOutputs>();
        for(FlowInputs request : requests) {
            results.add(handleLocalLogic(request));
        }
        return results;
    }

    // The function that handles all the logic
    public static FlowOutputs handleLocalLogic(FlowInputs request) {
        // Return variable
        FlowOutputs result = new FlowOutputs();

        /////////////// YOUR LOGIC GOES HERE ///////////////
        // Access input variables with: 
        // - request.input1 = ...
        // - request.input2 = ...
        // - request.input3 = ...
        // - etc...
        //
        // Run your logic here
        // - Make SOQL requests
        // - Call an API
        // - Create records
        // - Run complex business logic
        // - etc...
        //
        // Set output variables with: 
        // - result.output1 = ...
        // - result.output2 = ...
        // - result.output3 = ...
        // - etc...

        return result;        
    }

    // Input details that comes to apex from flow
    public class FlowInputs{

        @InvocableVariable
        public String input1;

        @InvocableVariable
        public String input2;

        @InvocableVariable
        public String[] input3;

    }

    // Output details which goes from apex to flow
    public class FlowOutputs{

        @InvocableVariable
        public String output1;

        @InvocableVariable
        public String output2;

        @InvocableVariable
        public Boolean output3;

    }
}

Adding the code to Salesforce

Create an Apex class

Now that we have generated our Apex code, let's add it to Salesforce. To do so, we will need to create a new Apex Class:

SF Developer Console Add Class

Make sure to set the name of the class to the same name we used when creating the Apex code:

SF Developer Console Class Name

Add our code

Now that we have our class generated, let's add our code from earlier:

SF Developer Console Add Code

Running the Invocable Function

The real power of this setup is when we call our new function from a Flow! Let's create a basic record-triggered Flow and add our new Invocable Function:

SF Flow Setup

Wow! That was easy :)

Conclusion

Invocable Apex is super easy to set up and will change the way you develop apex! Instead of writing your triggers in Apex, try writing them as record-triggered Flows - now you can add additional steps and change up the entrance criteria on the fly! Say you were asked to query an API and display that data on a Lightning page. Write an Invocable Function to return that data and use a Screen Flow to display it!

The possibilities are endless - if you like this, leave a comment on your use case and share how you used Invocable Functions in your Salesforce project!

Comments

Login to Add comments.