How to pass array of objects from Script Include to Client Script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-26-2022 04:11 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-26-2022 04:32 AM
Please see my comments above.
From Script Include I get:
[{"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"}]
Script Include:
var arr = [1, 2, 3, 4, 5];
var obj = {};
var arrStore = [];
var iteration = this.getParameter('sysparm_iteration');
var gr = new GlideRecord("abc_table");
gr.orderBy('u_iteration');
gr.addQuery('u_program_increment', iteration);
gr.query();
while (gr.next()) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == gr.getValue("u_iteration")) {
obj = {iteration:i,fromDateVal:gr.getValue('u_from_date'),toDateVal:gr.getValue('u_to_date')};
arrStore.push(obj);
//gs.info("Script Include: " + gr.getValue('u_program_increment') + " : " + gr.getValue("u_iteration") + " : " + gr.getValue('u_from_date') + " : " + gr.getValue('u_to_date'));
gs.info("arrStore:" + JSON.stringify(arrStore));
}
}
}
gs.info("arrStore value:" + arrStore);
var returnValue = JSON.stringify(arrStore);
return returnValue;
I need the same array of objects that is return from script include to be on my client script
Client Script:
var iterationVal = "22";
var ajax = new GlideAjax('SetFieldValues');
ajax.addParam('sysparm_name', 'setRecordValues');
ajax.addParam('sysparm_iteration', iterationVal);
ajax.getXML(getReturnValue);
function getReturnValue(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = JSON.parse(answer);
g_form.addInfoMessage(answer);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-26-2022 04:34 AM
Hi,
you are doing stringify() so it would come as string to client script
don't parse it; directly use
var iterationVal = "22";
var ajax = new GlideAjax('SetFieldValues');
ajax.addParam('sysparm_name', 'setRecordValues');
ajax.addParam('sysparm_iteration', iterationVal);
ajax.getXML(getReturnValue);
function getReturnValue(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.addInfoMessage(answer);
}
Regards
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
‎04-26-2022 04:41 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-26-2022 05:12 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-26-2022 08:15 AM
it's already an array of javascript object
what's your business requirement?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader