- 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-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-07-2016 08:21 AM
Thank you Chuck.
For the shake of information if try to send this object from script include to client script
var sendResponse = '{role:[{"id": "123", "name": "IT_Role"},{"id": "456", "name": "BUSINESS_Role", }]}';
var responseJONObject = new global.JSON().decode(sendResponse)
How do i send this responseJSONObject object to client script and get name from this objects.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2016 08:24 AM
Replace decode with encode on line 2. You are turning the object in to a string.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2016 08:12 AM
Post you code here. I can edit your code