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.

Json returns null

thecoywwlf
Kilo Contributor

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'

      });

//-------------------------------------------------------

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

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;


View solution in original post

13 REPLIES 13

It's Geneva.



We are currently developing in a sandbox/test environment. I'm not sure if this matters.


Thanks. I couldn't remember if stringify() was a Geneva or Helsinki thing.



Next question, is this part of a scoped app? If so, try adding "global" before it.



Ex: global.JSON.stringify()



Alternatively, use the older method.



gs.log(new global.JSON().encode(arr));


BINGO! It's a scoped app.



Thank you very much.


I suspected that earlier, but got off track. Sorry for the delay. Glad you got it figured out. Don't forget to use global. as a prefix to scripts includes and other elements that are outside of your app's scope.