Newer Version Available

This content describes an older version of this product. View Latest

Function Class

Use the Function class to access deployed Salesforce Functions, and invoke them synchronously or asynchronously.

Namespace

functions

Usage

The Function class represents an instance of a deployed Function you can invoke from your org. You can invoke Functions synchronously, or asynchronously using asynchronous Apex.

If your Function takes longer than 2 minutes to return, the request will time out. To avoid this, consider using asynchronous invocation. Invoking a Function asynchronously doesn’t count against asynchronous Apex limits, such as Apex Queueable limits.

Before synchronously invoking a Function, commit any pending data operations in Apex, otherwise you will get a CalloutException. For asynchronous invocations, the queued invocation will not happen if the Apex transaction is not committed. Any data operations that happen in the Function itself are not considered part of the Apex transaction.

Example

The following example synchronously invokes a deployed “AccountFunction” Function:

1functions.Function accountFunction = functions.Function.get('AccountFunction');
2functions.FunctionInvocation invocation = accountFunction.invoke('{ "accountName" : "Acct", "contactName" : "MyContact", "opportunityName" : "Oppty" }');
3String jsonResponse = invocation.getResponse();

The following example asynchronously invokes a deployed “AccountFunction” Function, using the provided callback:

1functions.Function accountFunction = functions.Function.get('AccountFunction');
2accountFunction.invoke('{ "accountName" : "Acct", "contactName" : "MyContact", "opportunityName" : "Oppty" }', new MyCallback());
3
4public class MyCallback
5  implements functions.FunctionCallback {
6    public void handleResponse(functions.FunctionInvocation result) {
7      // Handle result of function invocation
8      // ...
9    }
10}

Function Methods

The following are methods for Function.

get(functionName)

Returns the Function instance for the named Function. The Function must be deployed to the Salesforce Functions Space and App that your org is bound to and have appropriate permissions to work with the org running your Apex code.

Signature

public static functions.Function get(String functionName)

Parameters

functionName
Type: String
The name of the Salesforce Function. The Function must be deployed to the Salesforce Functions infrastructure, and your org must be bound to the Salesforce Functions Space and App the Function was deployed to.

Return Value

Type: functions.Function

Returns a Function instance that you can invoke.

invoke(payload, callback)

Invokes the Function asynchronously.

Signature

public functions.FunctionInvocation invoke(String payload, functions.FunctionCallback callback)

Parameters

payload
Type: String
The payload data that gets passed to the Function. Specify your payload data in a JSON-format string.
callback
Type: functions.FunctionCallback
A FunctionCallback implementation that gets called when your Function is invoked asynchronously.

Return Value

Type: functions.FunctionInvocation

Returns a FunctionInvocation that contains information about the results of the invocation, such as the Function response, or error results.

invoke(payload)

Invokes the Function synchronously.

Signature

public functions.FunctionInvocation invoke(String payload)

Parameters

payload
Type: String
The payload data that gets passed to the Function. Specify your payload data in a JSON-format string.

Return Value

Type: functions.FunctionInvocation

Returns a FunctionInvocation that contains information about the results of the invocation, such as the Function response, or error results.