Reuse GlideRecord in Loop? How?

William Busby
Tera Guru

I'm parsing a JSON object within a workflow that is supposed to cycle through a list of CI's and add comments to each one. The JSON object is an array of ServerName, Action, and Results for 'n' number of CIs. Parsing the JSON is good, but when I loop through the list the GlideRecord works on the first CI and no others (and doesn't throw an exception). Here's what I tried first:

doProcessResponse();

function doProcessResponse() {

  workflow.scratchpad.decommLog = "Server Decommission Log\n";

  //parse JSON string to object

  var result = new global.JSON().decode(activity.output);

  for ( var i=0; i < result.length; i++) {

  var serverGr = new GlideRecord('cmdb_ci');

  serverGr.addQuery('name', result[i].Server);

  serverGr.query();

  if (serverGr.next()) {

  var logEntry = result[i].Server + " decommission action " + result[i].Action + " results " + result[i].Result;

  gs.warn("logEntry " + logEntry);

  serverGr.comments = logEntry;

  serverGr.update();

  workflow.scratchpad.decommLog += logEntry + "\n";

  }

  // destroy GlideRecord object for reuse

  serverGr = null;

  }

  gs.warn("workflow.scratchpad.decommLog = " + workflow.scratchpad.decommLog);

}

So, knowing that objects created in functions are garbage collected and subsequent calls to the function create new objects I just moved all the logic into a function like this:

doProcessResponse();

function doProcessResponse() {

  workflow.scratchpad.decommLog = "Server Decommission Log\n";

  //parse JSON string to object

  var result = new global.JSON().decode(activity.output);

  for ( var i=0; i < result.length; i++) {

  doUpdates(result[i]);

  }

  gs.warn("workflow.scratchpad.decommLog = " + workflow.scratchpad.decommLog);

}

function doUpdates(result) {

  var serverGr = new GlideRecord('cmdb_ci');

  serverGr.addQuery('name', result.Server);

  serverGr.query();

  if (serverGr.next()) {

  var logEntry = result.Server + " decommission action " + result.Action + " results " + result.Result;

  gs.warn("logEntry " + logEntry);

  serverGr.comments = logEntry;

  serverGr.update();

  workflow.scratchpad.decommLog += logEntry + "\n";

  }

}

WTF??? Again only the first result object is processed!!!

1 ACCEPTED SOLUTION

William Busby
Tera Guru

Apologies to everyone who tried to help with this but the pain was entirely self-inflicted. Turns out the JSON object had a space char prepended to the server name EXCEPT for the first one. That extra space was causing the query to fail. Once I removed whitespace all is well. Yet another lesson in making assumptions.


View solution in original post

16 REPLIES 16

The script editor b!tches about WARNING at line 10: Do not use String as a constructor.


William Busby
Tera Guru

Apologies to everyone who tried to help with this but the pain was entirely self-inflicted. Turns out the JSON object had a space char prepended to the server name EXCEPT for the first one. That extra space was causing the query to fail. Once I removed whitespace all is well. Yet another lesson in making assumptions.