How to get outputs from subflow by script, when subflow gets into waiting state?

Iveta Prazakova
Mega Expert

Hi,

In our scope application, we need to run a Subflow via script and get outputs. So I used executeSubflow() method:

// Execute Synchronously: Run in foreground. Code snippet has access to outputs.
var outputs = sn_fd.FlowAPI.executeSubflow('subflow_name', inputs);

But when the Subflow gets into waiting state for some time, the method throws an exception 'The current execution is in the waiting state', so I can't get the outputs. It is also written in docs: https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/sn_fd-namespace/ScriptableFlo...

Do you know how to get outputs from subflow that gets into waiting state?

1 REPLY 1

Rich Dennis _CC
Tera Contributor

Here's how I got around this issue:

try {
    var inputs = {};
    inputs['user'] = "adde4468db816810a7970ad4e2961926"; // Sys ID (GUID)
    var contextId = sn_fd.FlowAPI.startSubflow('global.flow_name', inputs);
    var exit = false;
    var outputs = new GlideRecord('sys_flow_runtime_value');
    while (exit == false) {
        outputs.addQuery('context', contextId);
        outputs.addQuery('type', "OUTPUT");
        outputs.query();
        if (outputs.next()) {
            var outputJSON = JSON.parse(outputs.value);
            workflow.scratchpad.output_01 = outputJSON.output_01.displayValue;
            workflow.scratchpad.output_02 = outputJSON.output_02.displayValue;
            exit = true;
        } else {
            gs.sleep(5000);
        }
    }
} catch (ex) {
    workflow.scratchpad.error = ex.name;
}

It uses a while loop to check the table "sys_flow_runtime_value" for the OUTPUT record. The field "value" has the outputs stored as JSON, so you can parse out what you need.