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 timing out, 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 won’t 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.
Functions cannot be invoked in an Apex test. A “Function invocations from Apex tests are not supported” CalloutException is thrown if Apex determines that a Function is being invoked during a test. If you must run tests against code that invokes Functions you’ll need to mock your Function invocations during the tests. See FunctionInvocation Example Implementation for an example of a mocked FunctionInvocation that you can use in testing.
Example
The following example synchronously invokes a deployed “accountfunction” Function:
1functions.Function accountFunction = functions.Function.get('MyProject.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('MyProject.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 and the Functions Project that the Function is part of. The format of the parameter string is “project name.function name”. For example, to retrieve the generatepdf Function in the Onboarding Function Project, use Onboarding.generatepdf. The Function and Project must be deployed to a compute environment connected to the org.
Usage
- InvalidParameterValueException — The functionName parameter doesn’t have the correct project name.function name format.
- NoDataFoundException — The project or Function name provided in the functionName parameter couldn’t be found. Make sure the project and Function name are spelled correctly and that the project and Function have been properly deployed.
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.
Usage
-
CalloutException — One of the following
conditions causes this exception to be thrown:
- Salesforce Functions isn’t enabled on the current org. For more details on enabling Functions, see Configure Orgs for Functions in the Functions Developer Guide.
- The Function is being invoked in an Apex test. Functions can’t be invoked in tests.
- The “Functions” permission set is missing or has incorrect permissions for FunctionInvocationRequest. For more details on the correct permissions for FunctionInvocationRequest see Function Permissions in the Functions Developer Guide.
- The provided payload isn’t valid JSON.
- The Function hasn’t completed deployment to a compute environment or invocation request returns a 404 HTTP error.
- InvalidParameterValueException — The callback parameter is null or references a class that doesn’t implement functions.FunctionCallback.
- NoDataFoundException — A reference for the Function couldn’t be found in the current org. Make sure the project and Function have been properly deployed.
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.
Usage
-
CalloutException — One of the following
conditions causes this exception to be thrown:
- Salesforce Functions isn’t enabled on the current org. For more details on enabling Functions, see Configure Orgs for Functions in the Functions Developer Guide.
- The Function is being invoked in an Apex test. Functions can’t be invoked in tests.
- The provided payload isn’t valid JSON.
- There are pending DML operations.
- The Function is being synchronously invoked from an Apex trigger.
- The Function hasn’t completed deployment to a compute environment or invocation request returns a 404 HTTP error.
- The Function request returns a 5xx HTTP error.
- The Function invocation has exceeded the time limit for synchronous invocations. For details on the time limit and work-arounds, see Limits in the Functions Developer Guide.
- NoDataFoundException — A reference for the Function couldn’t be found in the current org. Make sure the project and Function have been properly deployed.