Converting XML to JSON fails with: Error: \"gs\" is not defined.,Detail: \"gs\" is not defined.

juliuspiv
Tera Contributor

I've created an Action that essentially takes an input, which contains XML and converts it to JSON using the following code:

(function execute(inputs, outputs) {
var res = gs.xmlToJSON(inputs.payload);
Outputs.custom_output = res.results.result.stdout;
})(inputs, outputs);

When testing the subflow and evaluating the subflow execution details, I can confirm the following

 

All steps leading up to the action that parses payload complete successfully:

juliuspiv_0-1769206928315.png

 

The payload runtime value is populated with properly formatted XML:

<?xml version="1.0" encoding="UTF-8"?><results probe_time="2372"><result command="powershell .\scripts\PowerShell\CustomPowerShellScript.ps1"><stdout>"ExpectedResult"</stdout><stderr/></result><parameters><parameter name="agent" value="mid.server.midservername"/><parameter name="signature" value=""/><parameter name="source" value=""/><parameter name="skip_sensor" value="true"/><parameter name="sys_id" value="somelongvalue"/><parameter name="from_host" value=""/><parameter name="sys_created_on" value="2026-01-23 21:49:25"/><parameter name="_msg_read" value="2026-01-23T13:49:16.503-0800"/><parameter name="sys_domain" value="global"/><parameter name="_msg_process_start" value="2026-01-23T13:49:16.505-0800"/><parameter name="state" value="ready"/><parameter name="_msg_response_queued" value="2026-01-23T13:49:18.877-0800"/><parameter name="_msg_process_end" value="2026-01-23T13:49:18.877-0800"/><parameter name="response_to" value=""/><parameter name="from_sys_id" value=""/><parameter name="priority" value="2"/><parameter name="agent_correlator" value=""/><parameter name="processed" value=""/><parameter name="error_string" value=""/><parameter name="sequence" value="19becd561b50000001"/><parameter name="name" value="powershell .\scripts\PowerShell\CustomPowerShellScript.ps1"/><parameter name="topic" value="Command"/><parameter name="_msg_submit_to_worker_pool" value="2026-01-23T13:49:16.505-0800"/><parameter name="queue" value="output"/><parameter name="ecc_queue" value="somelongvalue"/></parameters></results>

 

The runtime value of the action status shows: 

{
    "Action Status": {
        "code": 1,
        "message": "Error: \"gs\" is not defined.,Detail: \"gs\" is not defined."
    }
}

 

I also don't have any other code referencing gs.* further confirming it's specific to that call action.

 

Flow engine log entries

IPaaSActionProbe | Encountered error executing instruction: ActionErrorEvalInstruction{id=30, conditions=[], statusKey=c9o.__action_status__, dontTreatAsErrorKey=c9o.__dont_treat_as_error__}, errorMessage:Error: "gs" is not defined.,Detail: "gs" is not defined., errorCode:1

IPaaSActionProbe | Encountered error executing instruction: OpInstruction{id=28, opClass=com.snc.process_flow.operation.script.ScriptOperation, io=ReadOnlyDefaultOutputsIo{input={mid_selection=StringValue{fValue='auto_select'}, capabilities=StringValue{fValue=''}, connection_alias=StringValue{fValue=''}, application=StringValue{fValue='00bdbaee5b5e610375'}, mid_server=StringValue{fValue=''}, payload=FlowReference{value=flow_variable.payload}, mid_cluster=StringValue{fValue=''}, required_run_time=StringValue{fValue='mid'}, host=StringValue{fValue=''}, mid_selection_type=StringValue{fValue='any'}, script=StringValue{fValue='(function execute(inputs, outputs) {
var res = gs.xmlToJSON(inputs.payload);
Outputs.custom_output = res.results.result.stdout;
})(inputs, outputs);
'}}, output={custom_output=s3o.custom_output, __step_status__=s3o.__step_status__}, defaultOutput={}}, name=0a1ab9ba3bae7e90706c88ae53e45aa1, parentActionId=e1ca55f23b2e7e90706c88ae53e45aab}
com.snc.process_flow.exception.OpException: Error: "gs" is not defined.,Detail: "gs" is not defined.
	at com.snc.process_flow.operation.script.ScriptOperationBase.handleScriptResult(ScriptOperationBase.java:83)
	at com.snc.process_flow.operation.script.ScriptOperationBase.runScript(ScriptOperationBase.java:57)
	at com.snc.process_flow.operation.script.ScriptOperation.run(ScriptOperation.java:75)
	at com.snc.process_flow.engine.Operation.run2(Operation.java:90)
	at com.snc.process_flow.v2.exec.OpRunner.runOperation(OpRunner.java:74)
	at com.snc.process_flow.v2.exec.OpRunner.runOperation(OpRunner.java:68)
	at com.snc.process_flow.v2.exec.OpInstructionExecutor.runOperation(OpInstructionExecutor.java:62)
	at com.snc.process_flow.v2.exec.OpInstructionExecutor.execute(OpInstructionExecutor.java:29)
	at com.snc.process_flow.v2.exec.OpInstructionExecutor.execute(OpInstructionExecutor.java:16)
	at com.snc.process_flow.v2.exec.EngineV2.executeInstructions(EngineV2.java:183)
	at com.snc.process_flow.v2.exec.EngineV2.run(EngineV2.java:107)
	at com.snc.process_flow.v2.exec.EngineRunner.lambda$run$0(EngineRunner.java:28)
	at com.snc.process_flow.v2.exec.enviroment.execution.ExecutionScopeRunner.run(ExecutionScopeRunner.java:37)
	at com.snc.process_flow.v2.exec.EngineRunner.run(EngineRunner.java:28)
	at com.snc.process_flow.engine.EngineV2Adapter.runWithExecutionResult(EngineV2Adapter.java:160)
	at com.snc.process_flow.engine.EngineV2Adapter.resumeWithExecutionResult(EngineV2Adapter.java:443)
	at com.snc.process_flow.engine.ProcessAutomation.runWithExecutionResult(ProcessAutomation.java:107)
	at com.snc.process_flow.engine.ProcessAutomation.run(ProcessAutomation.java:66)
	at com.snc.process_flow.engine.ProcessAutomation.resume(ProcessAutomation.java:62)
	at com.snc.process_flow.engine.MidProcessAutomation.resumeFlow(MidProcessAutomation.java:72)
	at com.service_now.mid.probe.IPaaSActionProbe.lambda$probe$0(IPaaSActionProbe.java:129)
	at com.glide.util.LogContext$Frame.run(LogContext.java:247)
	at com.service_now.mid.probe.IPaaSActionProbe.probe(IPaaSActionProbe.java:99)
	at com.service_now.mid.probe.AProbe.process(AProbe.java:160)
	at com.service_now.mid.queue_worker.AWorker.runWorker(AWorker.java:187)
	at com.service_now.mid.queue_worker.AWorkerThread.run(AWorkerThread.java:20)
	at com.service_now.mid.threadpool.ResourceUserQueue$RunnableProxy.run(ResourceUserQueue.java:643)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:840)

 

 

Keywords: gs is not defined

1 ACCEPTED SOLUTION

Itallo Brandão
Tera Guru

Hi Julius,

The error Error: "gs" is not defined is the smoking gun here.

The Root Cause: Based on the stack trace (com.service_now.mid.probe.IPaaSActionProbe), your Script Action is attempting to execute on the MID Server.

The MID Server is a standalone Java application running on a remote server. It does not have access to the ServiceNow Instance's Global API (gs, GlideRecord, current, etc.). The gs object only exists when the script runs directly on the ServiceNow Instance.

The Solution: You need to force this specific Script Step to run on the Instance.

  1. Open your Custom Action.

  2. Click on the Script Step (where you wrote the code).

  3. Look for the Required Runtime setting (usually found in the step configuration or settings).

  4. Change it from MID (or "Inherited") to Instance.

Why this fixes it: By moving the execution to the "Instance", the script will run on the ServiceNow nodes where the gs global object is defined and available.

Bonus Code Tip: While gs.xmlToJSON works, the modern standard for this on the platform is XMLHelper. If you want a more robust parsing method, try this:

JavaScript
 
(function execute(inputs, outputs) {
    // XMLHelper is available on the Instance runtime
    var helper = new XMLHelper(inputs.payload);
    var obj = helper.toObject(); // Converts XML directly to a JS Object
    
    // Adjust the path below based on the resulting object structure
    if (obj && obj.result) {
         outputs.custom_output = obj.result.stdout;
    }
})(inputs, outputs);


If this response helps you solve the issue, please mark it as Accepted Solution.

Best regards, Brandão.

View solution in original post

2 REPLIES 2

Itallo Brandão
Tera Guru

Hi Julius,

The error Error: "gs" is not defined is the smoking gun here.

The Root Cause: Based on the stack trace (com.service_now.mid.probe.IPaaSActionProbe), your Script Action is attempting to execute on the MID Server.

The MID Server is a standalone Java application running on a remote server. It does not have access to the ServiceNow Instance's Global API (gs, GlideRecord, current, etc.). The gs object only exists when the script runs directly on the ServiceNow Instance.

The Solution: You need to force this specific Script Step to run on the Instance.

  1. Open your Custom Action.

  2. Click on the Script Step (where you wrote the code).

  3. Look for the Required Runtime setting (usually found in the step configuration or settings).

  4. Change it from MID (or "Inherited") to Instance.

Why this fixes it: By moving the execution to the "Instance", the script will run on the ServiceNow nodes where the gs global object is defined and available.

Bonus Code Tip: While gs.xmlToJSON works, the modern standard for this on the platform is XMLHelper. If you want a more robust parsing method, try this:

JavaScript
 
(function execute(inputs, outputs) {
    // XMLHelper is available on the Instance runtime
    var helper = new XMLHelper(inputs.payload);
    var obj = helper.toObject(); // Converts XML directly to a JS Object
    
    // Adjust the path below based on the resulting object structure
    if (obj && obj.result) {
         outputs.custom_output = obj.result.stdout;
    }
})(inputs, outputs);


If this response helps you solve the issue, please mark it as Accepted Solution.

Best regards, Brandão.

Good afternoon @Itallo Brandão and thank you for taking the time to provide me with a such a comprehensive reply!  Switching the runtime from MID Server to Instance and republishing solved the issue.  I am incredibly appreciative of you taking the time to share your wisdom with me and to share that new robust parsing method.