- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2016 11:06 AM
Hello,
I've followed the tutorial located at https://fruitionpartners.eu/blog/2015/11/17/glideajax-return-multiple-values-using-json/ but my JSON object still returns "null".
Is there something wrong with my script below? Thank you.
Serverside script
//----------------------------------
var MyBiCustomAjax = Class.create();
MyBiCustomAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
grantsBiQueries: function() {
var grantsBi = new GlideRecord('x_snc_grantsbi_grant');
grantsBi.orderByDesc('program_title'); //orders by program title
grantsBi.query();
var array = [];
while(grantsBi.next()) {
var object = {};
object.shortName = grantsBi.getDisplayValue('office_short_name');//Grabs all short names
object.cfda = grantsBi.getDisplayValue('cfda').toString();//Grabs numbers
array.push(object);
}
var json = new JSON();
var data = json.encode(array); //JSON formatted string
return data;
},
type: 'MyBiCustomAjax'
});
//-------------------------------------------------------
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2016 12:17 PM
A couple suggestions David.
First, rename 'array' to something else. I know 'Array' is a keyword, but try to avoid naming variables as keywords (even if they are case sensitive.) Name it something like "arr" or "myArray".
Second, Throw a debug statement in right after your grantsBi.query() to ensure you are counting the records properly. gs.log(grantsBi.getRowCount() + ' rows returned'); should provide some interesting output. You can find it in System Logs> System Log> Script Log Statements.
Third, change these lines:
var json = new JSON();
var data = json.encode(array); //JSON formatted string
return data;
to this
var answer = JSON.stringify(myArray); // or whatever you called array
gs.log('Here is the output: ' + answer);
return answer;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2016 12:14 PM
I would test the function in the background scripts and add some gs.log statements to see what is happening.
For example:
var grantsBi = new GlideRecord('x_snc_grantsbi_grant');
grantsBi.orderByDesc('program_title'); //orders by program title
grantsBi.query();
//lets see how many rows we get back
gs.log(grantsBi.getRowCount());
var array = [];
while(grantsBi.next()) {
var object = {};
object.shortName = grantsBi.getDisplayValue('office_short_name');//Grabs all short names
object.cfda = grantsBi.getDisplayValue('cfda').toString();//Grabs numbers
array.push(object);
}
var json = new JSON();
var data = json.encode(array); //JSON formatted string
//lets see if there is anything in the data object
gs.log(data);
Regards,

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2016 12:17 PM
A couple suggestions David.
First, rename 'array' to something else. I know 'Array' is a keyword, but try to avoid naming variables as keywords (even if they are case sensitive.) Name it something like "arr" or "myArray".
Second, Throw a debug statement in right after your grantsBi.query() to ensure you are counting the records properly. gs.log(grantsBi.getRowCount() + ' rows returned'); should provide some interesting output. You can find it in System Logs> System Log> Script Log Statements.
Third, change these lines:
var json = new JSON();
var data = json.encode(array); //JSON formatted string
return data;
to this
var answer = JSON.stringify(myArray); // or whatever you called array
gs.log('Here is the output: ' + answer);
return answer;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2016 02:09 PM
Both great answers so far. I'm definately familiar with the browser(chrome/firebug) debugging tools but haven't delved much into Servicenow. This will be my first introduction into SN debugging so will let you know how it goes tomorrow. Sorry. I feel so inept at SN right now. Oh also, great tutorials Chuck.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2016 02:31 PM
So I've narrowed down the culprit to the object in the loop.
var arr = [];
while(grantsBi.next()) {
var object = {};
object.shortName = grantsBi.getDisplayValue('office_short_name');//Grabs all short names
object.cfda = grantsBi.getDisplayValue('cfda').toString();//Grabs numbers
arr.push(object);
}
Result
The results of the loop above is:
[object Object],[object Object],[object Object]......
instead of
[
{"shortName":"OESE",
"cfda":"123"}
]
Am I doing something wrong?