Loading
Fazer uma chamada remota de execução longa usando VlocityContinuationIntegration

Fazer uma chamada remota de execução longa usando VlocityContinuationIntegration

Para oferecer suporte a chamadas remotas de execução longa, o Vlocity oferece suporte ao uso do objeto Continuação do Salesforce. A interface VlocityOpenInterface2 e a classe VlocityContinuationIntegration oferecem suporte a chamadas remotas normais e chamadas remotas que usam o objeto Continuation. Para as novas classes do Apex, não use VlocityOpenInterface.

Para obter mais informações sobre o objeto Continuação do SFDC, consulte a seguinte documentação do Salesforce:

Para fazer uma chamada remota usando o objeto Continuação do Salesforce e estendendo a classe VlocityContinuationIntegration:

  1. Crie uma classe do Apex que estenda a classe VlocityContinuationIntegration.

    Essa classe implementa VlocityOpenInterface2.

  2. Implemente o método de retorno no invokeMethod. Para obter mais informações, consulte a classe de amostra no final deste tópico.
  3. Para o método que retorna o Objeto de continuação, chame o método VlocitySetAsyncCallbackState antes do retorno.
    con.continuationMethod = 'customcallback'; // implemented in invokeMethod
    VlocitySetAsyncCallbackState(con, con.addHttpRequest(req), options);
  4. Para o último método de retorno de chamada na cadeia que retorna a resposta de volta ao OmniScript, use o código a seguir para obter o estado do objeto de continuação e dos rótulos do sistema.
    Object state = inputMap.get('vlcContinuationCallbackState');
    Object labels = inputMap.get('vlcContinuationCallbackLabels');
  5. O Objeto "state" retornado de inputMap.get('vlcContinuationCallbackState') deve ser um Map<String, Object> com conteúdo que não pode ser gerado diretamente usando (CustomApexWrapperClass)inputMap.get('vlcContinuationCallbackState'). Em vez disso, o objeto retornado deve ser serializado para JSON e então desserializado e gerado para sua classe correta. Por exemplo:
    CustomApexWrapperClass wrap = (CustomApexWrapperClass)JSON.deserialize(JSON.serialize(state), CustomApexWrapperClass.class);

    Essa abordagem permite que qualquer dado gravado no VlocitySetAsyncCallbackState seja retido e usado como sua classe do Apex personalizada.

    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;
       }
    }
 
Carregando
Salesforce Help | Article