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.

A function's org can be public and only access references within a namespace, or a function's org can be global and access references across multiple namespaces.

Retrieve a public 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 “MyProject.myfunction” to retrieve references within the same namespace. For example, to retrieve the "GeneratePDF" function in the "Onboarding" function project, use "Onboarding.GeneratePDF".

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

Leaving out the namespace defaults to the org the function is in. Accessing references across namespaces in a public org results in error messages.

Retrieve a global reference to a deployed function by using the name of the function, its project, and the unique id of the namespace where the reference is. Similarly, using the functions.Function.get() method, pass the project, function, and namespace to retrieve references from across namespaces.

The following Apex example gets a reference to the AccountFunction from the MyNamespace namespace, 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. Apex triggers must be called asynchronously. If you can't use asynchronous invocation, see Limits for other approaches.

The following 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 fails, 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 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.