0

I have a class that implements Process.Plugin. It calls the invoke method, which calls a future method, which calls a webservice. The returned value from the webservice should be the output parameters. My issue is the static variables do not seem to be retaining their value outside of the future method.

I assume this has something to do with Async vs Sync methods but if anyone could explain as to why the result variable isn't holding it's value after the webservice is completed, I would appreciate it!

global class FlowToWebservice_Class implements Process.Plugin {
   public static Map<String,Object> result;
   ...

global static Process.PluginResult invoke(Process.PluginRequest request){
    varProcessName = (String)request.inputParameters.get('varProcessName');
    varCreditReviewId = (String)request.inputParameters.get('varCreditReviewId');
    callWebService(varProcessName , varCreditReviewId);
    return new Process.PluginResult(result);//the value of `result` is null so this will never return output parameters
}

@future(callout=true)
global static void callWebService(String processName, String crId) {
    k = JSON.serialize(new Map<String, object> {'Parameters' => new Map<String, object> {'CreditReviewId' => crId},'RulesSet' => processName}); 
    req = new HttpRequest();
    req.setEndpoint(endpointURL);
    req.setMethod('POST');
    req.setBody(k);
    req.setHeader('Content-Type', 'application/json');
    req.setTimeout(120000);
    string d = getAuth();
    req.setheader('Authorization', d);
    try {
        Http h = new Http();
        res = h.send(req);
        JSONParser parser = JSON.createParser(res.getBody());
        while (parser.nextToken() != null) {
            ....
        }
    }
    catch (System.CalloutException z) {
        system.debug('z: ' + z);
    }
    result = new Map<String,Object>();
    result.put('varResult',finalDecision);
    result.put('varROSID', relatedOppSearchId);
    result.put('varDeclineReason', declineReason);
}
6

Static variables in Apex only retain their value through the course of a single transaction. Future methods by nature execute in a separate transaction, which means that your static variables are reset. You cannot use static variables to return a value from a future method to the synchronous code that called it.

  • ah interesting. Thank you for the quick response and the explanation. Because I can't return on a future method, and the variables are reset every time the method runs, how would I return the output parameters to the flow? (I have to break out the methods like this because it is being called from a Trigger). – Olivia Jan 12 '18 at 15:09
  • I am not a heavy user of Flows, so others might have a better architectural recommendation than I do. The only option that comes to my mind would be a Wait element in the Flow looking for a Platform Event that is later fired by your web service code. But if you're calling this from a trigger, you won't be able to pause and wait for a webservice return anyway. Why not fire the future method straight from the trigger? – David Reed Jan 12 '18 at 15:12
  • @DavidReed Do you have any links where it says we can pause a flow to wait for platform events? – Pranay Jaiswal Jan 12 '18 at 15:13
  • 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.