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.

10 REPLIES 10

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

There is no such tables, called sys_context or sys_context_variables.
I think its just AI generated answers for some bounty point.