Follow and complete a Learn MOAR Winter ’22 trailmix for Admins or Developers by October 31 to earn a special community badge and get a chance to win one of five Salesforce Certification vouchers (up to $200USD value). Restrictions apply. Learn how to participate and review the Official Rules by visiting the Learn MOAR quests page.

The Winter ’22 release brings exciting new features for Apex developers. In this post, we’ll give you a preview of three of our favorites: a new Enum valueOf() method, calling invokable actions from Apex, and using mock responses to test Salesforce Functions.

Enum valueOf() method

Let’s start by exploring the new valueOf() method in the Enum class. This method lets you convert a String to an Enum constant value. Before Winter’22, to get an Enum constant value from string, you had to iterate through all the values searching for a matching name. Rather than, for example, having to create a map linking string and Enum constants to do the lookup against, you can simply use Enum.valueOf().

This feature addresses an IdeaExchange request: Apex Enum parse from string.

Call invocable actions from Apex (developer preview)

The Winter’22 release incorporates the ability to call invocable actions from Apex. Invocable actions are automated actions that can be invoked from different entry points such as the REST API, Flow, or Process Builder. They are one of the best tools in Salesforce to quickly get things done. From Winter’22, you will be able to invoke them in Apex. This adds one more tool to the Apex developer toolset, making it easier and quicker to implement certain types of automation, to reuse code, and it builds one more bridge between low-code and pro-code development.

Bear in mind that this feature is in developer preview. You can activate it on scratch orgs by including the CallIAFromApex feature in the scratch org configuration file.

Let’s take a look at some examples. First, let’s explore how to invoke standard actions. Every button and link in Salesforce can be considered a standard action; take a look at a complete list of available standard actions. In this example we invoke a standard invocable action to create a Chatter post:

To call the action, we first instantiate the createStandardAction method from the new Invocable.Action Apex class. Then, we set the invocation parameters, and finally, we invoke it calling the invoke method. The method returns a list of Invocable.Action.Result that you can explore to find out if the operation succeeded or failed or retrieve the output.

This feature is not restricted to standard invocable actions; you can call custom invocable actions too. Custom invocable actions are defined in Apex. You specify the inputs, outputs, and what the method will do. For instance, here we have a custom invocable action that receives a mascot name as a parameter and returns a String, building a sentence:

Now we can instantiate the action with createCustomAction, set the expected parameter, and invoke it:

Take a look at all the methods available for the new Invocable.Action, Invocable.Action.Result and Invocable.Action.Error classes on the release notes!

Use mock responses to test Salesforce Functions

Finally, let’s go into our third topic, our beloved Functions! Functions are scheduled to become Generally Available in the Winter’22 release. As part of this release, we’ve created some classes and interfaces to help you to easily write Apex tests for function invocations.

Let’s take a look at some examples. We’ll use an arbitrary function just to illustrate how testing works. In fact, the code that we use here doesn’t matter as the mock will be responding during test execution rather than the underlying function.

To test the invocation of a function, you’ll have to provide a mock implementation for the function. The mock class needs to implement the new functions.FunctionInvokeMock interface and specify the mock response sent to the respond() method when the Apex runtime calls the function. By using the createSuccessResponse() and createErrorResponse() methods in the mock class, you can emulate the success or failure of the function invocation.

In this sample mock, by default we emulate a successful response. If the throwError variable is true, then we emulate an error.

Let’s take a look at a method in the SampleFunctionCall class that invokes the sample function synchronously:

The postToChatter() method posts a message to Chatter, indicating if the function executed successfully or if an error happened:

This is how we can test the callFunctionSynchronously() method:

In the example we instruct the Apex runtime to send the fake response by calling Test.setMock(). The first argument to Test.setMock() is the functions.FunctionInvokeMock.class, and the second argument is a new instance of your implementation of the functions.FunctionInvokeMock interface.

When the Test.setMock() method is called, the real function is not actually invoked. Instead, the mock response that you specified in the respond() method is received instead. In this case, we mocked a successful invocation, so a success Chatter post should have been created.

In the case of invoking a function asynchronously, you’ll have to specify a callback to process the function output, as seen in this second example:

The callFunctionAsynchronously() method, reuses the postToChatter() method showed above.

We can test the async function invocation in the same way we did for the sync one.

In this example, we force the error (by setting throwError to true), so that the function mock fires an error. Then we check that the error Chatter post has been created.

Now you have all the required tooling to start writing Functions! Take a look at our Functions Recipes sample app to learn more.

Next steps

If you want to take a deeper look at these new Apex features coming in Winter ‘22, check the rr-winter22 GitHub repository.

About the author

Alba Rivas works as a Principal Developer Advocate at Salesforce. She focuses on Lightning Web Components and Lightning adoption strategy. You can follow her on Twitter @AlbaSFDC.

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS