Return multiple values from Glide Ajax
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2014 06:33 AM
Hi all,
I have a requirement like to return multiple values from script include at a time from glide ajax call and I have to store those values in different variables.
Can anyone please let me know how to write a script.
I am calling Script include from Client script Glide ajax
- Labels:
-
Service Mapping
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2014 06:35 AM
Try here: GlideAjax - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2015 09:12 AM
Hi msrishilpa,
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. From the server, you would encode the data to a JSON formatted string. From the client, you would retrieve the JSON formatted string and decode it into a javascript object.
Below, the detailed steps in both server and client side scripts :
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 : 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
--
Check out this article to see 2 other exemples: return a simple array or return an array of objects.
Hope it helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2016 10:10 PM
Nice. Much simpler than returning multiple nodes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2017 08:34 AM
Thank you Ximizu for sharing, this is really nice and a lot more simple than previous solutions I saw on the wiki, docs and other posts on the community.
On a requirement I had over here, I used this solution to get a couple of data fields from the company table by using a GlideRecord on the server side.
/*
** Client side Client Script
*/
function onLoad() {
var cp = g_form.getValue('company');
//g_form.addInfoMessage('cp is: ' + cp);
var ga = new GlideAjax('GetCompanyParms');
ga.addParam('sysparm_name', 'getCompanyParms');
ga.addParam('sysparm_company', cp.toString());
ga.getXML(cpCB);
}
function cpCB( response ) {
var answer = response.responseXML.documentElement.getAttribute("answer");
//g_form.addInfoMessage('answer is: ' + answer);
answer = answer.evalJSON();
//g_form.addInfoMessage(answer);
if (answer.active == 'false') {
g_form.addErrorMessage('Company ' + answer.name + ' is inactive. -> ' + answer.active);
}
}
---------------------------------------------------------------------------------------------------------------------------------
/*
** Server side Script Include
*/
var GetCompanyParms = Class.create();
GetCompanyParms.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCompanyParms : function() {
var obj = {};
obj.name = "";
obj.active = "";
var retData = "";
var comp = this.getParameter('sysparm_company');
var gr = new GlideRecord('core_company');
gr.addQuery('sys_id' , comp);
gr.query();
if ( gr.next() && gr.u_active == false ) {
obj.name = gr.name.toString();
//gs.log('obj.name is: ' + obj.name);
obj.active = gr.u_active.toString();
//gs.log('obj.active is: ' + obj.active);
}
var json = new JSON();
retData = json.encode(obj);
//gs.log('retData is: ' + retData);
return retData;
},
type: 'GetCompanyParms'
});
-------------------------------------------------------------------------------------------------------------------------------------
Thanks again!
Marcelo Correia