Script include returning "org.mozilla.javascript.NativeArray"

Naga23
Giga Expert

Hi Techies,

My requirement is to populate "database" variable , the values of this "database" variable are populate based on another variable "Landscape". So I wrote below onChange catalog client script and script include.

Script include returning something like "org.mozilla.javascript.NativeArray" . Please find below scripts and let me know what causing this issue.

Catalog Client Script:

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

var ga = new GlideAjax('ITSM_Database_oracle_choices');
ga.addParam('sysparm_name','getdatabase');
ga.addParam('sysparm_land_name',newValue);
ga.getXML(UpdateChoiceValues);

function UpdateChoiceValues(response) {
var strOption = [];
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer != null) {
strOption = answer.split(',');
}
g_form.clearOptions('database');
g_form.addOption('database', '', '-- None --');
for(var i=0;i<strOption.length;i++) {
g_form.addOption('database', strOption[i], strOption[i]);
}
}
}

Script Include:

 

var ITSM_Database_oracle_choices = Class.create();
ITSM_Database_oracle_choices.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getdatabase:function(){

var land = this.getParameter('sysparm_land_name');

var c = new GlideRecord('u_catalog_approvals_and_routing');

c.addEncodedQuery('u_catalog_item=a267a495db63c01002e258eadc961943^u_catalog_item=a267a495db63c01002e258eadc961943^u_field_name=database');
c.addQuery('u_dependent', land);
c.query();
while(c.next())
{
var ar = [];
var str = c.u_name.toString();
ar.push(str);

}
var answer = ar;
return answer;

},

type: 'ITSM_Database_oracle_choices'
});

9 REPLIES 9

Tanaji Patil
Tera Guru

Thats because when you send date from server to client (or vice-versa) it considers the returned object as String. Objects cannnot be transmitted over internet (http).

So you need to send it as any form of string (including JSON string) and then parse (or split) it on other end.

I see you are doing split on the client side so that good.

Only change you need is to replace last line in your script include function i.e.

return answer;

to

return answer.toString();

 

Hope this will solve your issue!

-Tanaji

Please mark response correct/helpful if applicable

 

hello Sir This is very helpful to me. Could you please explain what needs to be done in client script side?

Actually from my script include I am returning an array say arr1=["30","260"]. Now when I get the result in my console.log I see 30,260 but I don't know how to separate them.

Mike225
Tera Expert

Server Side Change:

var answer = ar;
return answer;

to

var answer = JSON.stringify(ar);
return answer;


Client Side Change: 

strOption = answer.split(',');

to

strOption = JSON.parse(answer);

 

Explanation:

Everything is an object in JS, even Arrays.

var a = [1,2,3]; 
typeof (a) // returns "object"

JSON stringify is an easy way to pass it through as a string and convert it back to an array afterwords. The GlideAjax calls do not like passing non strings through as the response answer.

Akhil40
Giga Contributor

Hey Mike,

The changes you suggested works fine for me, thanks

Mike..

Works Perfect...