Interested in learning more about ServiceNow with practical examples? Visit my channel, Runjay Patel, for in-depth insights!
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. The best way to return data from server to client is 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.
Note: As you guys all know, ServiceNow has introduced the concept of Scoped Application since the Fuji release. In Scoped Application, new JSON().evalJSON() and new JSON().encode() are not available, it only works in the Global Scope.
You can use below in case you are working in a Scoped application.
-
- 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()
RETURN AN OBJECT OR ARRAY USING JSON / Access Script Includes From Client Script
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. Then the solution 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.
Below are the detailed steps mentioned for both server and client side script with 3 scenarios.
-
- Return a simple object.
- Return a simple array.
- Return an array of objects.
Server-side steps
-
- Prepare the data you want to send to the client. Data can be contained in a simple object or an array.
- Instantiate a JSON object and encode the data to be sent.
- Return the data. The data sent to the client is in a JSON formatted string.
Client side Steps
-
- Retrieve the data.
- Decode the JSON formatted string into an object or array, which contains multiple values.
- Process the values regarding the business requirements.
Scenario 1: Return a Simple Object
In this scenarios asset(alm_asset) table has been used, you can use based on your requirement.
Sample Data to be returned.
{
“model”:” P1000185 – Apple MacBook Pro 15″”,
“model_category”:” Computer”
}
Server Side Code
var AjaxUtils = Class.create(); AjaxUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, { returnSimpleObject: function() { var obj = {}; var sysId = this.getParameter('sysparm_sys_id'); var almAssetGr = new GlideRecord("alm_asset"); almAssetGr.addQuery('sys_id', sysId); almAssetGr.query(); if (almAssetGr.next()) { obj.model = almAssetGr.getDisplayValue('model'); obj.model_category = almAssetGr.getDisplayValue('model_category'); var json = new JSON(); var data = json.encode(obj); //JSON formatted string } return data; }, type: 'AjaxUtils' });
Client Side Code
function onLoad() { var ga = new GlideAjax('AjaxUtils'); ga.addParam('sysparm_name', 'returnSimpleObject'); ga.addParam('sysparm_sys_id', g_form.getValue('sys_id')); ga.getXML(showMessage); } function showMessage(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); g_form.addInfoMessage('JSON String: '+answer); //JSON String answer = answer.evalJSON(); //Transform the JSON string to an object g_form.addInfoMessage('Object: '+ answer); g_form.addInfoMessage('Asset Model: '+answer.model); //Display Asset Model g_form.addInfoMessage('Asset Category: '+answer.model_category); //Display Asset Category }
Result
Scenario 2: Return a Simple Array
In this scenarios asset(alm_asset) table has been used, you can use based on your requirement.
Sample Data to be returned.
[” P1000185 – Apple MacBook Pro 15″”,
” Computer”,
]
Server Side Code
var AjaxUtils = Class.create(); AjaxUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, { returnSimpleArray: function() { var obj = []; var sysId = this.getParameter('sysparm_sys_id'); var almAssetGr = new GlideRecord("alm_asset"); almAssetGr.addQuery('sys_id', sysId); almAssetGr.query(); if (almAssetGr.next()) { obj[0] = almAssetGr.getDisplayValue('model'); obj[1] = almAssetGr.getDisplayValue('model_category'); var json = new JSON(); var data = json.encode(obj); //JSON formatted string } return data; }, type: 'AjaxUtils' });
Client Side Code
function onLoad() { var ga = new GlideAjax('AjaxUtils'); ga.addParam('sysparm_name', 'returnSimpleArray'); ga.addParam('sysparm_sys_id', g_form.getValue('sys_id')); ga.getXML(showMessage); } function showMessage(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); g_form.addInfoMessage('JSON String: '+answer); //JSON String answer = answer.evalJSON(); //Transform the JSON string to an object g_form.addInfoMessage('Object: '+ answer); for( var i=0 ; i < answer.length ; i++) { //Loop into the array g_form.addInfoMessage(answer[i]); } }
Result
Scenario 3: Return an Array Of Objects
In this scenarios asset(alm_asset) table has been used, you can use based on your requirement.
Sample Data to be returned.
[{
“model”:” P1000185 – Apple MacBook Pro 15″”,
“model_category”:” Computer”
},
{
“model”:” P1000188 – Apple MacBook Pro 15″”,
“model_category”:” Computer”
},
{
“model”:” P1000472 – Apple MacBook Pro 15″”,
“model_category”:” Computer”
},
]
Server Side Code
var AjaxUtils = Class.create(); AjaxUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, { returnSimpleJSONObject: function() { var obj = []; var sysIds = '1bc1fa8837f3100044e0bfc8bcbe5d48,1fc1fa8837f3100044e0bfc8bcbe5d50,ffa96c0d3790200044e0bfc8bcbe5d87'; var almAssetGr = new GlideRecord("alm_asset"); almAssetGr.addEncodedQuery('sys_idIN'+ sysIds); almAssetGr.query(); while (almAssetGr.next()) { var payload = {}; payload.model = almAssetGr.getDisplayValue('model'); payload.model_category = almAssetGr.getDisplayValue('model_category'); obj.push(payload); } var json = new JSON(); var data = json.encode(obj); //JSON formatted string return data; }, type: 'AjaxUtils' });
Client Side Code
function onLoad() { var ga = new GlideAjax('AjaxUtils'); ga.addParam('sysparm_name', 'returnSimpleJSONObject'); ga.getXML(showMessage); } function showMessage(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); g_form.addInfoMessage('JSON String: '+answer); //JSON String answer = answer.evalJSON(); //Transform the JSON string to an object g_form.addInfoMessage('Object: '+ answer); for( var i=0 ; i < answer.length ; i++) { //Loop into the array g_form.addInfoMessage(answer[i].model + " - " + answer[i].model_category); } }
Result
Simple and excellent article with apt examples
Thanks for your feedback.