You are here:
Make a Long-Running Remote Call Using VlocityContinuationIntegration
To support long-running remote calls, Vlocity supports the use of the Salesforce Continuation object. The VlocityOpenInterface2 interface and VlocityContinuationIntegration class support normal Remote Calls and Remote Calls that use the Continuation Object. For the new Apex classes, don’t use VlocityOpenInterface.
For more information on the SFDC Continuation object, see the following Salesforce documentation:
To make a remote call using the Salesforce Continuation object and extending the VlocityContinuationIntegration class:
-
Create an Apex class that extends the
VlocityContinuationIntegration class.
This class implements VlocityOpenInterface2.
- Implement the callback method in the invokeMethod. For more information, refer to the sample class at the end of this topic.
-
For the method that returns the Continuation Object, call the method
VlocitySetAsyncCallbackState before the return.
con.continuationMethod = 'customcallback'; // implemented in invokeMethod VlocitySetAsyncCallbackState(con, con.addHttpRequest(req), options); -
For the last callback method in the chain that returns the response back to
Omniscript, use the following code to get the state of the Continuation Object and system
labels.
Object state = inputMap.get('vlcContinuationCallbackState'); Object labels = inputMap.get('vlcContinuationCallbackLabels'); -
The 'state' Object returned from
inputMap.get('vlcContinuationCallbackState')must be aMap<String, Object>with contents that can’t be directly cast using(CustomApexWrapperClass)inputMap.get('vlcContinuationCallbackState'). Instead, the returned object must be serialized to JSON and then deserialized and cast to its correct class. For example:CustomApexWrapperClass wrap = (CustomApexWrapperClass)JSON.deserialize(JSON.serialize(state), CustomApexWrapperClass.class);This approach permits any data written to the VlocitySetAsyncCallbackState to be retained and used as its custom Apex class.
Sample Class VlocityContinuationIntegrationTest.cls
// Sample Apex class for making Remote Call in OmniScript // (1) Create a custom Apex class which extends VlocityContinuationIntegration, for a Vlocity managed package, // need to include the Namespace prefix - NS.VlocityContinuationIntegration // (2) implement invokeMethod, return type is Object (a) in the Continuation Object case, return Continuation Object // (b) in the normal case, you can return Boolean global with sharing class VlocityContinuationIntegrationTest extends VlocityContinuationIntegration { global override Object invokeMethod(String methodName, Map<String, Object> inputMap, Map<String, Object> outMap, Map<String, Object> options) { Boolean result = true; try { // the custom methods can have any customized signature, but // PLEASE MAKE USRE YOU ALWAYS PASS IN options if(methodName.equals('Continuation1')) { // this returns Continuation Object return Continuation1(10, inputMap, outMap, options); } else if(methodName.equals('Continuation2')) { return Continuation2(8, options); } else if(methodName.equals('customcallback')) { customcallback(inputMap, outMap, options); } // other methods to handle normal Remote Call else { result = false; } } catch(System.Exception e) { // System.log ... result = false; } return result; } // You can have other parameters as well, but make sure you always pass in Map<String, Object> options private Object Continuation1(Integer count, Map<String, Object> inputMap, Map<String, Object> outMap, Map<String, Object> options) { // THIS IS A SAMPLE TO CALL HEROKU NODE SERVICE // Make an HTTPRequest as we normally would // Remember to configure a Remote Site Setting for the service! String url = 'https://node-count.herokuapp.com/'+count; >HttpRequest req = new HttpRequest(); req.setMethod('GET'); req.setEndpoint(url); // Create a Continuation for the HTTPRequest Continuation con = new Continuation(60); // // Please set up callback method here // (1) Include this callback method in the above invokeMethod, refer to // else if(methodName.equals('Continuation2')) // (2) methodName is CASE SENSITIVE con.continuationMethod = 'Continuation2'; // The following method MUST BE CALLED // params: // (1) first parameter = Continuation Object // (2) second parameter = WHATEVER YOU WANT TO SET THE state parameter of the CONTINUAION Object // https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_continuation_process.htm // (3) third parameter = options // what it does: // restructure the state property of the Continuation Object to be passed to the next Callback // con.state is a Map, which contains: // (a) vlcContinuationCallbackState - the state you want to set, in this example, con.addHttpRequest(req) // (b) vlcContinuationCallbackLabels - labels, refer to // https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_continuation_process.htm VlocitySetAsyncCallbackState(con, con.addHttpRequest(req), options); // Return it to the system for processing return con; } // callback for the first Continuation // Returns Continuation Object // This is to show you how to serialize multiple long running remote calls private Object Continuation2(Integer count, Map<String, Object> options) { // EXAMPLE // Make an HTTPRequest as we normally would // Remember to configure a Remote Site Setting for the service! String url = 'https://node-count.herokuapp.com/'+count; HttpRequest req = new HttpRequest(); req.setMethod('GET'); req.setEndpoint(url); // Create a Continuation for the HTTPRequest Continuation con = new Continuation(60); // (1) This callback method should be included in the above invokeMethod, refer to above // else if(methodName.equals('customcallback')) con.continuationMethod = 'customcallback'; // The following line has to be called VlocitySetAsyncCallbackState(con, con.addHttpRequest(req), options); // Return it to the system for processing return con; } // Last callback method // This does NOT return Continuation Object // This will return the response back to OmniScript, therefore need to set outMap private Object customcallback(Map<String, Object> inputMap, Map<String, Object> outMap, Map<String, Object> options) { // This is how you access the state and labels passed by the Continuation Object Object state = inputMap.get('vlcContinuationCallbackState'); Object labels = inputMap.get('vlcContinuationCallbackLabels'); // EXAMPLE HttpResponse response = Continuation.getResponse((String)state); Integer statusCode = response.getStatusCode(); if (statusCode >= 2000) { // System.log ... } // This goes back to OmniScript outMap.put('continuousResp', response.getBody()); return null; } }

