Naveen20
ServiceNow Employee

Add a Script step in the subflow before the Look Up Record action, parse out the actual number, set it into a flow variable, and use that variable in the Look Up Record.

Here's the walkthrough:

Step 1 — Create a new flow variable

Open your subflow, go to the flow variables section, and create a new variable:

  • Name: parsed_control_number
  • Type: String

Step 2 — Add a Script step

Add a Script Step (Flow Logic → Script) as the very first action in your subflow, before the Look Up Record. Use this script:

(function execute(inputs, outputs) {

    var raw = inputs.control_number; // your subflow input variable name

    if (raw) {
        raw = raw.toString().trim();

        // If AI Agent Studio wrapped it in JSON, extract the value
        if (raw.indexOf('{') === 0) {
            try {
                var parsed = JSON.parse(raw);
                raw = parsed.input || parsed.number || '';
            } catch (e) {
                // Not valid JSON, use as-is
            }
        }

        // Strip any extra quotes that may remain
        raw = raw.replace(/^"|"$/g, '');
    }

    outputs.parsed_number = raw || '';

})(inputs, outputs);

In the Script step configuration, map the input to your subflow input (the control number), and define an output variable called parsed_number of type String.

Step 3 — Set the flow variable

After the Script step, add a Set Flow Variables step (or do it inline if your version supports it):

  • Set parsed_control_number = Script Step → parsed_number

Step 4 — Update Look Up Record

In your Look Up Record action, change the filter condition from using the raw subflow input to using the flow variable instead:

  • Table: sn_compliance_control
  • Condition: number is fd_data.subflow_inputs.parsed_control_number

(Or pick it from the data pill picker under Flow Variables → parsed_control_number)

Step 5 — Test

Run the test again. The Script step will strip the JSON wrapper, and the Look Up Record will receive just CTRL0021175 as a clean string.

If after this it's still passing the JSON object, add a gs.info('RAW INPUT: ' + raw) line at the top of the script to log what's actually arriving — that'll confirm the exact format being sent and you can adjust the parsing accordingly.

View solution in original post