Update: 28/08/17
Since the Fuji release, ServiceNow has introduced the concept of Scoped Application.
When working in Scoped Application, the methods new JSON().evalJSON() and new JSON().encode() are not available, but only in the Global Scope.
If you work on a Scoped application, you need to use the following methods:
To change a object to a string, use JSON.stringify(myObject) instead of new JSON().encode(myObject).
To change a string to an object, you may use JSON.parse(myObject) instead of myObject.evalJSON()
When you are in a client script and need to retrieve information at the database level, the best practice is to use GlideAjax. In some cases, you would need to retrieve multiple values. To achieve this, you could do as explained in the wiki : return multiple XML nodes and attributes from the server. But there is a better alternative: return data using JSON . Doing this way, the data sent from the server is better structured and the script is easier to maintain as it is more comprehensible for the developer.
RETURN AN OBJECT OR ARRAY USING JSON
Ideally, in order to send multiple values, it would be nice if we could pass objects or arrays from the server to the client. But this is not possible when using GlideAjax. The solution then is to use a JSON object in order to:
– Encode the data from the server into a JSON formatted string
– Retrieve the JSON formatted string from the client and decode it into a javascript object
Below, the detailed steps in both server and client side scripts followed by 3 examples: return a simple object, return a simple array, return an array of objects.
Server side
1. Prepare the data you want to send to the client. Data can be contained in a simple object or an array.
2. Instantiate a JSON object and encode the data to be sent.
3. Return the data. The data sent to the client is in a JSON formatted string.
Client side
4. Retrieve the data
5. Decode the JSON formatted string into an object or array, which contains multiple values.
6. Process the values regarding the business requirements.
EXAMPLE 1: RETURN A SIMPLE OBJECT
Data to be returned
{ "var1":"Hello", "var2":"World" }
Server Side
var MyCustomAjax = Class.create(); MyCustomAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, { helloWorld: function() { var obj = {}; obj.var1 = 'Hello'; obj.var2 = 'World'; var json = new JSON(); var data = json.encode(obj);//JSON formatted string return data; }, type: 'MyCustomAjax' });
Client Side
function onLoad() { var ga = new GlideAjax('MyCustomAjax'); ga.addParam('sysparm_name', 'helloWorld'); ga.getXML(showMessage); } function showMessage(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); g_form.addInfoMessage(answer); //JSON String answer = answer.evalJSON(); //Transform the JSON string to an object g_form.addInfoMessage(answer); g_form.addInfoMessage(answer.var1); //Display "Hello" g_form.addInfoMessage(answer.var2); //Display "World" }
Result

EXAMPLE 2: RETURN A SIMPLE ARRAY
Data to be returned
[
« Hello »,
« World 2 »
]
Server Side
var MyCustomAjax = Class.create(); MyCustomAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, { helloWorld2: function() { var customArray = ['Hello', 'World 2']; var json = new JSON(); var data = json.encode(customArray); //JSON formatted string return data; }, type: 'MyCustomAjax' });
Client Side
function onLoad() { var ga = new GlideAjax('MyCustomAjax'); ga.addParam('sysparm_name', 'helloWorld2'); ga.getXML(showMessage2); } function showMessage2(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); g_form.addInfoMessage(answer); //JSON String answer = answer.evalJSON(); //Transform the JSON string to an object g_form.addInfoMessage(answer); for( var i=0 ; i < answer.length ; i++) { //Loop into the array g_form.addInfoMessage(answer[i]); } }
Result

EXAMPLE 3: RETURN AN ARRAY OF OBJECTS
Data to be returned
[ { "callerName":"Carol Coughlin", "number":"INC0000055", "shortDescription":"SAP Sales app is not accessible" }, { "callerName":"Luke Wilson", "number":"INC0000048", "shortDescription":"Having problems with Sales Tools performance" }, { "callerName":"Joe Employee", "number":"INC0000047", "shortDescription":"Issue with email" } ]
Server Side
var MyCustomAjax = Class.create(); MyCustomAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, { threeLastIncidents: function() { var incident = new GlideRecord('incident'); incident.orderByDesc('sys_created_on'); incident.setLimit(3); incident.query(); var array = []; while(incident.next()) { var object = {}; object.number = incident.getDisplayValue('number'); object.callerName = incident.getDisplayValue('caller_id'); object.shortDescription = incident.getDisplayValue('short_description'); array.push(object); } var json = new JSON(); var data = json.encode(array); //JSON formatted string return data; }, type: 'MyCustomAjax' });
Client Side
function onLoad() { var ga = new GlideAjax('MyCustomAjax'); ga.addParam('sysparm_name', 'threeLastIncidents'); ga.getXML(showMessage3); } function showMessage3(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); g_form.addInfoMessage(answer); //JSON String answer = answer.evalJSON(); //Transform the JSON string to an object g_form.addInfoMessage(answer); for( var i=0 ; i < answer.length ; i++) { //Loop into the array g_form.addInfoMessage(answer[i].number + " - " + answer[i].callerName + " - " + answer[i].shortDescription); } }
Result
