How to pass array of objects from Script Include to Client Script?

Hari1
Mega Sage

Hi,

I am getting the output as array of objects as below on the Script Include.

var arrStore:[{"iteration":1,"fromDateVal":"2022-04-25","toDateVal":"2022-04-25"},{"iteration":2,"fromDateVal":"2022-04-26","toDateVal":"2022-04-26"},{"iteration":3,"fromDateVal":"2022-04-27","toDateVal":"2022-04-27"},{"iteration":4,"fromDateVal":"2022-04-28","toDateVal":"2022-04-28"}]

I need to get this data on the client script.

Thanks.

9 REPLIES 9

Sourabh26
Giga Guru

Hi,

 

This is an array of objects. You can simply use below code to fetch the data.

var arr = [{
	"iteration": 1,
	"fromDateVal": "2022-04-25",
	"toDateVal": "2022-04-25"
}, {
	"iteration": 2,
	"fromDateVal": "2022-04-26",
	"toDateVal": "2022-04-26"
}, {
	"iteration": 3,
	"fromDateVal": "2022-04-27",
	"toDateVal": "2022-04-27"
}, {
	"iteration": 4,
	"fromDateVal": "2022-04-28",
	"toDateVal": "2022-04-28"
}];

for (var objs in arr){
    var data = arr[objs];
    gs.info(data.iteration);
    gs.info(data.fromDateVal);
    gs.info(data.toDateVal);
}

//Output
/*
*** Script: 1
*** Script: 2022-04-25
*** Script: 2022-04-25
*** Script: 2
*** Script: 2022-04-26
*** Script: 2022-04-26
*** Script: 3
*** Script: 2022-04-27
*** Script: 2022-04-27
*** Script: 4
*** Script: 2022-04-28
*** Script: 2022-04-28
*/

Mark this as Helpful/Correct, if Applicable.

 

Regards,

Sourabh

I have the output as below on the script include

[{"iteration":1,"fromDateVal":"2022-04-25","toDateVal":"2022-04-25"},{"iteration":2,"fromDateVal":"2022-04-26","toDateVal":"2022-04-26"},{"iteration":3,"fromDateVal":"2022-04-27","toDateVal":"2022-04-27"},{"iteration":4,"fromDateVal":"2022-04-28","toDateVal":"2022-04-28"}]

I need this data to be passed to the client script.

Script Include:

//Where arrStore is [{"iteration":1,"fromDateVal":"2022-04-25","toDateVal":"2022-04-25"},{"iteration":2,"fromDateVal":"2022-04-26","toDateVal":"2022-04-26"},{"iteration":3,"fromDateVal":"2022-04-27","toDateVal":"2022-04-27"},{"iteration":4,"fromDateVal":"2022-04-28","toDateVal":"2022-04-28"}]

var returnValue = JSON.stringify(arrStore);
return returnValue;

Client Script:

var answer = response.responseXML.documentElement.getAttribute("answer");
answer = JSON.parse(answer);
gs.addInfoMessage(answer);

I don't see anything in the infomessage

Hi,

 

Do changes on your client script as below.

 



var answer = response.responseXML.documentElement.getAttribute("answer");
var arr = JSON.parse(answer);
for (var objs in arr){
    var data = arr[objs];
    g_form.addInfoMessage(data.iteration);
    g_form.addInfoMessage(data.fromDateVal);
    g_form.addInfoMessage(data.toDateVal);
}
//You can use alert() as well to see the value on the form

 

Mark this as Helpful/Correct, if Applicable.

 

Regards.

Sourabh

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

what are you returning from script include?

the JSON object can be parsed in client side

what do you wish to obtain from that object?

Regards
Ankur

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