Invoke Functions with Apex

Salesforce Functions only run when invoked. When none are invoked, the Function rests in an idle state and elastically scales down to use minimal system resources. Functions can be invoked directly from a Salesforce org via Apex. The function and project must be deployed to a compute environment connected to the org, as described in Deploying a Function.

You can use Apex to invoke your functions synchronously or asynchronously. Functions can be invoked from anywhere you can use Apex code. So, for example, you could invoke or asynchronously queue your function within workflows that use Apex Actions in Flow, Batch Jobs, Apex Triggers, or Apex Platform Event Subscriptions.

Use classes in the functions Apex namespace to find and invoke deployed functions. Reference documentation for the Apex functions namespace and classes is available in the Apex Reference Guide: Functions Namespace.

Deploy your Apex code to your scratch org using the following command:

In VS Code, from the Command Palette, choose SFDX: Push Source to Default Scratch Org.

Retrieve a reference to a deployed function by using the name of the function and its project. Using the functions.Function.get() method, pass the Project and Function in a single string of the format “project name.function name”. For example, to retrieve the "GeneratePDF" Function in the "Onboarding" Function Project, use "Onboarding.GeneratePDF".

The following Apex example gets a reference to the AccountFunction deployed as part of the MyProject Project:

Use Synchronous Invocation

Use Function.invoke(payload) to invoke your function synchronously. The function invokes with the payload you provide, and response data and results are returned from the invoke() call in a FunctionInvocation instance.

For synchronous invocations, you have a maximum limit of 2 minutes for your Function to run and complete. If your function requires more than 2 minutes to complete, consider using asynchronous invocation instead. If you can't use asynchronous invocation, see Limits for other approaches.

The follow example synchronously invokes the accountFunction reference:

Use Asynchronous Invocation

Invoking a function directly through Apex is similar to making an Apex callout. If your function takes longer than 2 minutes to return, the request times out. To avoid time outs consider invoking your function asynchronously, using the asynchronous Function.invoke(payload, callback) method rather than the Function.invoke(payload) synchronous method. Provide an Apex callback class that implements functions.FunctionCallback in your call to Function.invoke(payload, callback).

Calling Function.invoke(payload, callback) queues your Function to be invoked through asynchronous Apex. When your queued Function is invoked, your Apex callback's handleResponse() method is called with the invocation results. Your method processes the FunctionInvocation results, which could include actions such as modifying org data, sending platform events, or sending custom notifications. If the Function invocation failed, error information is provided in the FunctionInvocation results.

The following example invokes a function asynchronously and uses a FunctionCallback class to handle the invocation results:

The Apex Callback runs as the user that invoked the function and runs in system mode.

Invoking a function asynchronously doesn’t count against asynchronous Apex limits, such as Apex Queueable limits.

To get the status of all pending functions invoked asynchronously, you can use a query on FunctionInvocationRequest similar to the following:

You can get the status for a specific asynchronous invocation by using the ID returned from FunctionInvocation.getInvocationId() as a filter on FunctionInvocationRequest:

Object NameLets the user...
  • Represent invocation information for a Salesforce Function.
  • When using the Apex functions.Function invoke methods, a FunctionInvocationRequest record is created that contains information on the status and results of the invocation.
  • Represent a deployed Salesforce Function associated with an org.
  • Use as a read-only record to get information about a specific Function associated with your org.
  • Represent a connection between an org and Salesforce Functions.
  • Use as a read-only object that shows the current connection information between your org and Salesforce Functions.