Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Iterate through variables in server side script

Mark Endsley
Tera Guru

I am trying to use the 'Submit a Record Producer' REST API to create functionality to copy a ticket.

 

This ticket is in a table where records can be created using multiple record producers, so the variables on the table can always be different.

 

So I want to iterate through current.variables, to grab all of the variables on that ticket, not knowing in advance what they may be. Normally I would just stringify the object and loop through it, but JSON.stringify is throwing this error:

 

Evaluator.evaluateString() problem: java.lang.NullPointerException: com.glide.catalog.scripting.rhino.impl.GlideElementScriptingModel$2.put(GlideElementScriptingModel.java:501)
com.glide.script.fencing.ScopedRhinoObjectWrapper.wrap(ScopedRhinoObjectWrapper.java:58)
com.glide.script.fencing.ScopedRhinoObjectWrapper.wrap(ScopedRhinoObjectWrapper.java:27)
com.glide.script.fencing.WrappedScriptableObject.get(WrappedScriptableObject.java:55)
com.glide.script.fencing.ScopedGlideElement.get(ScopedGlideElement.java:47)
org.mozilla.javascript.ScriptableObject.getProperty(ScriptableObject.java:2318)
org.mozilla.javascript.NativeJSON.str(NativeJSON.java:277)
org.mozilla.javascript.NativeJSON.jo(NativeJSON.java:380)
org.mozilla.javascript.NativeJSON.str(NativeJSON.java:338)
org.mozilla.javascript.NativeJSON.jo(NativeJSON.java:380)
org.mozilla.javascript.NativeJSON.str(NativeJSON.java:338)
org.mozilla.javascript.NativeJSON.stringify(NativeJSON.java:269)
org.mozilla.javascript.NativeJSON.execIdCall(NativeJSON.java:104)
org.mozilla.javascript.IdFunctionObject.call(IdFunctionObject.java:102)
org.mozilla.javascript.ScriptRuntime.doCall2(ScriptRuntime.java:2678)
org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:2617)
org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1518)
org.mozilla.javascript.Interpreter.interpret(Interpreter.java:830)
org.mozilla.javascript.InterpretedFunction.lambda$call$0(InterpretedFunction.java:152)
com.glide.caller.gen.sys_ui_action_0fcd817e1b5ce59044b95247624bcb9f_script.call(Unknown Source)
com.glide.script.ScriptCaller.call(ScriptCaller.java:18)
org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:151)
org.mozilla.javascript.ScriptRuntime.doCall2(ScriptRuntime.java:2678)
org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:2617)
org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1518)
org.mozilla.javascript.Interpreter.interpret(Interpreter.java:830)
org.mozilla.javascript.InterpretedFunction.lambda$call$0(InterpretedFunction.java:152)
com.glide.caller.gen.sys_ui_action_0fcd817e1b5ce59044b95247624bcb9f_script.call(Unknown Source)
com.glide.script.ScriptCaller.call(ScriptCaller.java:18)
org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:151)
org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:563)
org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3459)
org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:164)
com.glide.script.ScriptEvaluator.execute(ScriptEvaluator.java:359)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:182)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:115)
com.glide.script.GlideRhinoHelper.evaluateAsString(GlideRhinoHelper.java:138)
com.glide.script.ActionScript.conditionalEval(ActionScript.java:109)
com.glide.script.ActionScript.execute(ActionScript.java:94)
com.glide.script.ActionScriptProcessor.processScript(ActionScriptProcessor.java:217)
com.glide.script.Action.process(Action.java:117)
com.glide.ui.RedirectTransaction.invokeAction(RedirectTransaction.java:545)
com.glide.ui.RedirectTransaction.handleActions(RedirectTransaction.java:501)
com.glide.ui.RedirectTransaction.inboundActions(RedirectTransaction.java:337)
com.glide.ui.RedirectTransaction.process(RedirectTransaction.java:200)
com.glide.ui.GlideServletUITransaction.process(GlideServletUITransaction.java:100)
com.glide.processors.AProcessor.runProcessor(AProcessor.java:612)
com.glide.processors.AProcessor.processTransaction(AProcessor.java:274)
com.glide.processors.ProcessorRegistry.process0(ProcessorRegistry.java:184)
com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:172)
com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:46)
com.glide.sys.Transaction.run(Transaction.java:2488)
com.glide.ui.HTTPTransaction.run(HTTPTransaction.java:27)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
java.base/java.lang.Thread.run(Thread.java:829)

 

 

I know this is probably because of the Rhino engine, but what is the alternative to loop through all variables if you don't know their names ahead of time?

3 REPLIES 3

Amit Gujarathi
Giga Sage
Giga Sage

HI @Mark Endsley ,
I trust you are doing great.
To loop through all variables in a ticket without knowing their names ahead of time in ServiceNow, you can use the GlideElement API to retrieve the variables dynamically. Here's a solution that avoids the error you encountered with the Rhino engine:

// Get the current ticket record
var gr = new GlideRecord('<table_name>');
gr.get('<sys_id>');

// Loop through all variables
var variables = [];
var fieldNames = gr.getFields();
while (fieldNames.next()) {
  var fieldName = fieldNames.getName();
  if (fieldName.startsWith('variable_')) {
    var value = gr.getValue(fieldName);
    variables.push({
      name: fieldName,
      value: value
    });
  }
}

// Print the variables
for (var i = 0; i < variables.length; i++) {
  var variable = variables[i];
  gs.info('Variable name: ' + variable.name);
  gs.info('Variable value: ' + variable.value);
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hi Amit,

 

Thanks for your response, I think this gets me most of the way there. The issue I'm now running into is this.

 

MarkEndsley_0-1686830720449.png

Is there a scoped alternative for this?

Shivling
Tera Contributor

Hi Mark/All,
Please try code below:

 

var gr = new GlideRecord("table_name");
if(gr.get("sys_id")){
  for(var variableName in gr.variables){

         if (variableName == "variable_set_name"){
           for (var variableSet_variableName in gr.variables[variableName]){
              gs.info ("Variable Set Variable name: " +  variableSet_variableName );
              gs.info ("Variable Set Variable Value: " + gr.variables[variableName][variableSet_variableName].getDisplayValue());
            }
        } else{
         gs.info("Variable name: " + variableName );
         gs.info("Variable Value: " + gr.variables[variableName].getDisplayValue());
      }

  }
}