Send array from script include to client script.

anand98900
Kilo Contributor

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.

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

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      


View solution in original post

8 REPLIES 8

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?


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


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

 

francescodotti
Mega Expert

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'


});