Calling Invocable Actions from Apex

Overview

A subset of invocable actions are now callable from Apex code. This capability was previously Developer Preview and is now GA in the Winter ‘23 release.

Customer Benefit

Apex developers now have the ability to call a subset of standard and custom actions directly from their Apex code, bringing the diverse capabilities of invocable actions to Apex developers.

Capabilities and Usage

  • Invocable actions that do their own commit operations cannot be called from Apex.
  • The following standard actions are callable from Apex in Winter ‘23:
    • apex, flow
    • chatterPost
    • runExpressionSet, runDecisionMatrix
    • activateSessionPermSet, deactivateSessionPermSet
    • getAssessmentSummary
  • Additional standard actions will be callable from Apex in future releases!

This Apex code snippet uses the standard invocable action “chatterPost” to post a message to the current user’s feed:

Invocable.Action action = Invocable.Action.createStandardAction('chatterPost');
action.setInvocationParameter('text', 'This is a test.');
action.setInvocationParameter('type', 'User');
action.setInvocationParameter('subjectNameOrId', UserInfo.getUserId());
List<Invocable.Action.Result> results = action.invoke();
if (results.size() > 0 && results[0].isSuccess()) {
    System.debug('Created feed item with ID: ' + 
results[0].getOutputParameters().get('feedItemId'));

This code snippet calls a custom invocable action named “Doubler” that returns a number that is double the input value:

Invocable.Action action = Invocable.Action.createCustomAction('apex', 'Doubler');
action.setInvocationParameter('input', 1);
List<Invocable.Action.Result> results = action.invoke();                                          
if (results.size() > 0 && results[0].isSuccess()) {
    System.debug('Result is: ' + results[0].getOutputParameters().get('output'));