Split an Array from GlideAjax on the Client Side

Sarah50
Kilo Expert

Hello,

I'm fairly new to GlideAjax and I can't figure out how to pass an array from a Script Include via GA into a Client Script so that I can split out the values in my array to set fields on the form.

My script include is successfully logging the array - e.g.: MyArray fc3e0ba67b549400341bc676e84d4de2,No,Yes

What I can't figure out is how to get my client script to split out the values in the array so I can set form fields with the values.
I've tried several ways but my alert(answer) returns this: org.mozilla.javascript.NativeArray@xxxxxx (where xxxxxx is some random mix of numbers and letters which changes every time. Can i not pass a simple string array? Does the array need to be in XML format or something?

What is the correct way to process the array on the Client Side? Here's my current script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

if (isLoading){
return;
}

if(newValue){
var major = g_form.getReference('parent');
var ga = new GlideAjax('getParentFields');
ga.addParam('sysparm_name','ParentFields');
ga.addParam('sysparm_sys_id', major.sys_id);
ga.getXML(getFieldsParce);
}
function getFieldsParce(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
var resp = answer.split(',');
var bs = resp.split[0];
var ans1 = resp.split[1];
var ans2 = resp.split[2];

//set values on form with array results...
g_form.setValue('u_business_service', bs);
g_form.setValue('u_answer_1', ans1);
g_form.setValue('u_answer_2', ans2);
}
}

7 REPLIES 7

Brad Tilton
ServiceNow Employee
ServiceNow Employee

You only need to use the split method once, then set the elements to variables, so instead of:



var resp = answer.split(',');
var bs = resp.split[0];
var ans1 = resp.split[1];
var ans2 = resp.split[2];

use:


var resp = answer.split(',');
var bs = resp[0];
var ans1 = resp[1];
var ans2 = resp[2];


Sarah50
Kilo Expert

Thanks Brad, I tried that too and it still doesn't work. If I alert on one of my splits (e.g. alert(bs); , it just returns "org.mozilla.javascript.NativeArray@xxxxxx"
string.

-sarah


Hi Sarah,



I am also getting same thing. did you find any solution for this?


can you please guide me on this if any



Thanks,


Pavan.


Try this (at least to see what's in there):



var resp = answer.split(',');  


var bs = resp[0].toString();  


var ans1 = resp[1].toString();  


var ans2 = resp[2].toString();



I tried following the link provided by john.roberts without success.