Newer Version Available

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

FunctionInvocation Interface

Use FunctionInvocation to get the status and results of a synchronous or asynchronous Function invocation.

Namespace

functions

Usage

The results of a Function invocation are passed back via FunctionInvocation. Use this instance to determine if the invocation was successful, and any results from the Function invocation.

You can also implement your own FunctionInvocation interface if you run Apex tests with your Function invocation code. Your test code can create and use your own FunctionInvocation instance in place of using the results from a call to Function.invoke().

FunctionInvocation Methods

The following are methods for FunctionInvocation.

getError()

Returns error information for a Function invocation.

Signature

public functions.FunctionInvocationError getError()

Return Value

Type: functions.FunctionInvocationError

Contains a FunctionInvocationError instance that you can use to get information about any invocation errors. If the Function was invoked successfully, the returned instance is null.

getInvocationId()

Returns the invocation ID of the Function invocation.

Signature

public String getInvocationId()

Return Value

Type: String

This ID is available after a call to either the synchronous or asynchronous Function.invoke() methods. For asynchronous invocations, this ID can be used to check the status of the queued invocation.

getResponse()

Returns the response string of the Function invocation.

Signature

public String getResponse()

Return Value

Type: String

The response string is the raw request JSON response, which can be parsed using the JSONParser Class.

getStatus()

Returns the status of the Function invocation.

Signature

public functions.FunctionInvocationStatus getStatus()

Return Value

Type: functions.FunctionInvocationStatus

The result of the invocation, such as FunctionInvocationStatus.SUCCESS or FunctionInvocationStatus.ERROR.

FunctionInvocation Example Implementation

This is an example implementation of the functions.FunctionInvocation interface.

1public class MyFunctionInvocationError
2  implements functions.FunctionInvocationError {
3    public String getMessage() {
4      return 'Mock error message for testing';
5    }
6    public functions.FunctionErrorType getType() {
7      return functions.FunctionErrorType.FUNCTION_EXCEPTION;
8    }
9}
10
11public class MyFunctionInvocation
12  implements functions.FunctionInvocation {
13    public functions.FunctionInvocationStatus getStatus() {
14      return functions.FunctionInvocationStatus.ERROR;
15    }
16    public String getResponse() {
17      return 'Mock response for testing';
18    }
19    public String getInvocationId() {
20      return 'MOCKTESTID';
21    }
22    public functions.FunctionInvocationError getError() {
23      functions.FunctionInvocationError testError = new MyFunctionInvocationError();
24      return testError;
25    }
26}

The following example tests the implementation:

1functions.FunctionInvocation testInvocation = new MyFunctionInvocation();
2if (testInvocation.getStatus() == functions.FunctionInvocationStatus.ERROR) {
3    System.debug('Error: ' + (testInvocation.getError() != null ? testInvocation.getError().getMessage() : 'UNKNOWN'));
4    return;
5}