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

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);

    }

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

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

I see below in the screen

find_real_file.png

Hi Ankur,

By performing the above code change I am able to get the output but it is a string.

find_real_file.png

I need this to be as an array. Can you please help.

it's already an array of javascript object

what's your business requirement?

Regards
Ankur

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