- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2016 08:02 AM
I am trying to send array from script include to client script and traverse that array.
But i am unable to get an array in client script.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2016 08:05 AM
You need to encode your array in the script include and return a JSON string.
// Helsinki
return JSON.stringify(arrayName);
// Pre-Helinki
return new JSON().encode(arrayName);
In your client script, you parse the answer from the response like you normally would, but when you extract your array from the answer like this
var myArray = answer.evalJSON();
Docs: Client Scripts
Docs: GlideForm
Docs: GlideAjax
Client Script Best Practices - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2016 01:07 AM
Hi
Below is my script include -
var hardCoadedResponse = '{role:[{"id": "123", "name": "IT_Role", "owner": "Michael", "desc": "TestRole"},
{"id": "456", "name": "BUSINESS_Role", "owner": "John", "desc": "NewRole2"}]}';var responseJONObject = new global.JSON().decode(hardCoadedResponse);
var roleObject = responseJONObject.role;
I want to send this "roleObject" to client script and get the values from this object in client script.
var name = roleObject.name;
How can i do this in client script?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2016 05:54 AM
Hi Anand,
I thought we had this answered already. Based on your JSON above, your object contains an array called role. In your example, I see two objects as the array elements. hardcodedResponse.role[0] and hardCodedResponse.role[1]. To send the entire object "encoded" to your client script, use code like this in the script include:
var hardCodedResponse = '{role:[{"id": "123", "name": "IT_Role", "owner": "Michael", "desc": "TestRole"},
{"id": "456", "name": "BUSINESS_Role", "owner": "John", "desc": "NewRole2"}]}';
var responseString = new global.JSON().encode(hardCodedResponse);
return responseString;
In the client script, you'll have a standard GlideAjax setup and call with a callback function. In the callback function, first parse out the 'answer', then 'decode' your object.
var answer = response.responseXML.documentElement.getAttribute('answer');
var myObject = answer.evalJSON();
alert(myObject.role[0].name);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-11-2018 12:46 PM
Hi Abinay, All,
Maybe you can help me. I have tried Chuck and Francesco's suggestion but neither worked for me.
I have script include and a client script and the script include returns (468c66aedb190b8427ebf3541d96191d;I'm in HR. How do I run a Personnel Cost Planning Forecast?) which is sys id and record short description in the answer variable is what I expected.
In the client script I am trying to spit this at the ; but nothing I have tried works.
for example I add
function ajaxResponse(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
Myarr[];
Myarr = answer.split(';');
for (i = 0; i < Myarr.length; i++) {
var SysID = Myarr[0];
var Desc = Myarr[1];
var url ='https://instance.service-now.com/kb?id=kb_article_view&sys_kb_id='+SysID;
// var link = Desc + ' ' +'<a href="' + url + '">' + notes.substring(0,28) + '</a>' + ' ' + '\n' + '\n' + 'Additional comments:';
g_form.setValue('close_notes', url);
}
}
The script doesn't run or rather stops at the Myarr
Any idea why this is not working?
Thanks,
Eli
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-28-2017 08:31 AM
Hi Anand,
for a simple array, I think that the simplest solution is the following:
Client Side:
var prenot = new GlideAjax('IncludeName');
prenot.addParam('sysparm_name','functionName');
prenot.addParam('sysparm_param',paramValue);
prenot.getXML(clientFunction);
function clientFunction(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var fields= [];
fields = JSON.parse(answer);
g_form.setValue("_best_team",fields[0]);
g_form.setValue("u_naughty_guys",fields[1]);
...
}
Server Side: (Script Include)
var IncludeName = Class.create();
IncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
clientFunction:function() {
var campi = [];
campi.push("Go Milan !!");
campi.push("Down Internazionale !!");
var jsonCampi = JSON.stringify(campi);
return jsonCampi;
},
type: 'IncludeName'
});