Need to return multiple values from script include.

Swati44
Kilo Expert

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

6 REPLIES 6

Ahmmed Ali
Mega Sage

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

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

The simplest is often the best, thank you!

manoj6699
Giga Contributor

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