Newer Version Available
Function Class
Namespace
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)
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.
invoke(payload, callback)
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)
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.