
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2016 12:09 PM
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!!!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2016 11:52 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2016 02:51 PM
I've never tried using "new global.JSON().decode(activity.output);"
Have you tried using JSONParser?
Although "activity.output" suggests it's already an object.
Again, not really a fix, but this would help with the logging/debug (there is an open debate over pushing and appending for performance):
(function() {
var aDecommLog = [];
aDecommLog.push('Server Decommission Log');
//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++) {
aDecommLog.push('[' + i + '][' + result[i].Server + '][' + result[i].Action + '][' + result[i].Result + ']');
doUpdates(result[i]);
}
workflow.scratchpad.decommLog = aDecommLog.join('\n');
gs.warn("workflow.scratchpad.decommLog = " + workflow.scratchpad.decommLog);
function doUpdates(result) {
aDecommLog.push('[doUpdates][' + result.Server + '][' + result.Action + '][' + result.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;
aDecommLog.push(result.Server + ' decommission action ' + result.Action + ' results ' + result.Result);
//gs.warn("logEntry " + logEntry);
serverGr.comments = logEntry;
serverGr.update();
//workflow.scratchpad.decommLog += logEntry + "\n";
}
}
})();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2016 04:34 AM
I'd originally been using JSONParser but tripped over the link below and decided to adopt since we're unsure about the future of application namespaces within our environment. I'll take a look at your code but unless I can get the GlideRecord reuse implemented this whole thread is moot. I've opened a support ticket since what I'm trying to do seems perfectly 'normal' and any reasonable code base should support it.
FujiForty - What Happened to JSONParser? — CAVUCode
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2016 05:49 AM
I faced this issue some time back. what i did is creating a new string in every iteration explicitly. Can you try below?
var logEntry = new String(result[i].Server + " decommission action " + result[i].Action + " results " + result[i].Result);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2016 06:02 AM
Thx for the reply ram, but the issue is the GlideRecord doesn't succeed with the 'query' statement so nothing following the 'if' executes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2016 06:21 AM
Oh! Can you please try to put in "addQuery" function and check.
serverGr.addQuery('name', new String(result[i].Server));