Retrieve subflow input values through script

Dazler
Mega Sage

Hi,

 

I am trying to create a script that will return the input values from a subflow that has completed.  I don't have a reason for this, I just am playing around and wanted to see if it could be done.

 

If anyone has any ideas, any help would be appreciated.

9 REPLIES 9

Hi @Dazler,

In ServiceNow, the variables for a flow or subflow are not stored directly in the sys_flow_context table but in related tables. Specifically, you can find the variables in the sys_context and sys_context_variable tables.

Thank you, please make helpful if you accept the solution. 

Hi @Yashsvi 

 

I am trying your script include and business rule, but the results that is return is undefined.

Hi @Dazler,

To retrieve subflow input values through a script in ServiceNow, you need to access the sys_flow_context and sys_context tables. The sys_context table holds references to the sys_flow_context and contains the variable values used in the flow or subflow. Here's a script that retrieves these values:

  1. Script Include to handle the retrieval:

 

var SubflowUtils = Class.create();
SubflowUtils.prototype = {
    initialize: function() {},

    getSubflowInputs: function(flowContextId) {
        var inputs = {};
        var contextGR = new GlideRecord('sys_context');
        if (contextGR.get(flowContextId)) {
            var contextVarGR = new GlideRecord('sys_context_variable');
            contextVarGR.addQuery('sys_context', contextGR.sys_id);
            contextVarGR.query();
            while (contextVarGR.next()) {
                inputs[contextVarGR.name] = contextVarGR.value;
            }
        }
        return inputs;
    },

    type: 'SubflowUtils'
};

 

2.Scheduled Job:

 

(function() {
    var gr = new GlideRecord('sys_flow_context');
    gr.addQuery('state', 'complete'); // Adjust the query as needed
    gr.query();

    while (gr.next()) {
        var subflowUtils = new SubflowUtils();
        var inputs = subflowUtils.getSubflowInputs(gr.sys_id);
        gs.info('Subflow Inputs for Flow Context ' + gr.sys_id + ': ' + JSON.stringify(inputs));
    }
})();

 

  • The SubflowUtils script include has a method getSubflowInputs that takes a flow context ID and retrieves the associated input values from the sys_context_variable table.
  • It constructs an object where the keys are the variable names and the values are the variable values.
  • This scheduled job queries the sys_flow_context table for completed subflows (adjust the query as needed).
  • For each completed subflow, it retrieves the input values using the SubflowUtils script include and logs them.
  • Ensure you have proper permissions and roles to access the sys_flow_context, sys_context, and sys_context_variable tables and execute the Script Include.

Thank you, please make helpful if you accept the solution.

Hi @Yashsvi ,

 

The sys_context and sys_context_variable tables are not valid tables.  See errors.

 

Screenshot 2024-06-30 070312.png

 

Screenshot 2024-06-30 070326.png

Diogo Ramos
Giga Sage

Hello Dazler, I will leave my answer here as I had the same question. 

If you want to find all flows that had a specific input value at runtime you first need the reporting property for flows to enabled (com.snc.process_flow.reporting.level) (ONLY DO THIS IN NONPROD ENVIRONMENTS) and then you can use the table sys_flow_value table.

Note that to read the values, you need to update the READ ACL on that table, to allow admin to read the data.

Once you can read the data, use the following query: "nameSTARTSWITHin"

This will give you a list of inputs for all flow contexts, now you just have to filter on the flow you want via the context name or just use name of your input field (with underscores instead of spaces) you can use that as well:

DiogoRamos_1-1737125838653.png

Then you will see the values that were used if you check the value column, it has the content stored in JSON.

I tested this for subflows in XANADU, but I assume flows will be similar.

Cheers
Diogo