- 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-31-2016 04:08 AM
You are building the object. You cannot directly print objects in JavaScript. If you want to see the contents, use this as a sample debug statement.
gs.log(JSON.stringify(arr));
JSON.stringify() turns your object/array in to a printable string (also used in HTTP requests such as REST, SOAP, or Ajax.)
Docs: JSON
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2016 06:14 AM
Thanks. I get it. However when I try to stringify() the object into a string, I get the error message below on my log.
java.lang.IllegalArgumentException: Invalid queue id: eb9b83904f411200d9246bd18110c7c7: java.lang.RuntimeException: java.lang.IllegalArgumentException: Invalid queue id: eb9b83904f411200d9246bd18110c7c7: com.glide.rest.handler.impl.ServiceHandlerImpl.handleInvocationTargetException(ServiceHandlerImpl.java:77) com.glide.rest.handler.impl.ServiceHandlerImpl.invokeService(ServiceHandlerImpl.java:48) com.glide.rest.processors.RESTAPIProcessor.process(RESTAPIProcessor.java:217) com.glide.processors.AProcessor.runProcessor(AProcessor.java:409) com.glide.processors.AProcessor.processTransaction(AProcessor.java:183) com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:165) com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:32) com.glide.sys.ServletTransaction.run(ServletTransaction.java:34) java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) java.lang.Thread.run(Thread.java:682) |

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2016 06:32 AM
Can you share the code that is generating that error?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2016 07:12 AM
The code is below.
I can log everything up to (JSON.stringify(arr)) then I get the error I sent you.
I can gs.log/gs.info the array(arr). That's how I saw the [object,Object]. I can see the shortname and cfda values in seperate arrays with gs.info/log. So I know they have values. Also, is it just me or does gs.log not work everytime. gs.info definately does for me.
Let me know what you think.
/*********************CODE***************************************/
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.setLimit(3);
grantsBi.query();
var arr = [];
while(grantsBi.next()) {
var obj = {};
obj.shortName = grantsBi.getDisplayValue('office_short_name');//Grabs all short names
obj.cfda = grantsBi.getDisplayValue('cfda');
arr.push(obj);
}
//gs.info("test456" + arr); works. i see objects.
//gs.log("test456" + JSON.stringify(arr));
gs.log(JSON.stringify(arr));
//return answer;
},
/***********************************************************/
type: 'MyBiCustomAjax'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2016 07:14 AM
What version of ServiceNow are you on?