Need to return multiple values from script include.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2018 12:00 AM
Hi All,
I need to return multiple values from a script include to onChange client script. I have tried to return object and an array but no luck. Please suggest how to achieve this.
Thanks,
Swathi
- Labels:
-
Service Portal Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2018 12:02 AM
Hello,
please go through below link.
https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference/r_ExamplesOfAsynchronousGlideAjax.html
Returning multiple values
Since the response is an XML document we are not limited to returning a single answer
value. Here is a more complex example returning multiple XML nodes and attributes.
AJAX processor script include
/* * MyFavoritesAjax script include Description - sample AJAX processor returning multiple value pairs */ var MyFavoritesAjax = Class.create(); MyFavoritesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, { /* * method available to client scripts call using: * var gajax = new GlideAjax("MyFavoritesAjax"); * gajax.addParam("sysparm_name","getFavorites"); */ getFavorites: function() { // build new response xml element for result var result = this.newItem("result"); result.setAttribute("message","returning all favorites"); //add some favorite nodes with name and value attributes this._addFavorite("color","blue"); this._addFavorite("beer","lager"); this._addFavorite("pet","dog"); }, // all items are returned to the client through the inherited methods of AbstractAjaxProcessor _addFavorite: function(name, value) { var favs = this.newItem("favorite"); favs.setAttribute("name",name); favs.setAttribute("value",value); }, type:"MyFavoritesAjax" });
Client script
// new GlideAjax object referencing name of AJAX script include var ga = new GlideAjax("MyFavoritesAjax"); // add name parameter to define which function we want to call // method name in script include will be getFavorites ga.addParam("sysparm_name","getFavorites"); // submit request to server, call ajaxResponse function with server response ga.getXML(ajaxResponse); function ajaxResponse(serverResponse) { // get result element and attributes var result = serverResponse.responseXML.getElementsByTagName("result"); var message = result[0].getAttribute("message"); //check for message attribute and alert user if(message) alert(message); //build output to display on client for testing var output = ""; // get favorite elements var favorites = serverResponse.responseXML.getElementsByTagName("favorite"); for(var i = 0; i < favorites.length; i ++) { var name = favorites[i].getAttribute("name"); var value = favorites[i].getAttribute("value"); output += name + " = " + value + "\n "; } alert(output); }
Thanks,
Ali
Thank you,
Ali
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2018 12:05 AM
Hi Swathi,
Following script should help you:
Script Include:
var obj = {};
obj.var1 = 'Hello';
obj.var2 = 'World';
return JSON.stringify(obj);
Client Script:
var obj = JSON.parse(answer);
alert(obj.var2);
alert(obj.var1);
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-26-2021 07:49 AM
The simplest is often the best, thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2018 03:20 AM
Hi Swathi,
if u want to return multiple sys_id's use return 'sys_idIN'+kk.sys_id.toString(); // Use toString or else it will give u only one record
else if u want to return multiple field values push all the different field values in to a string or an array
Method 1:
var sk;
if(sk==''){
sk = kk.fieldName;
}
else{
sk = sk+','+kk.fieldName;
}
Method 2:
var sk = [];
sk.push(sk.filedName.toString());//Use toString() or else it will show only one value