Getting error org.mozilla.javascript.NativeArray

Community Alums
Not applicable

I have a requirement where I want to fetch all the computer names from cmdb_ci_computer table that are assigned to requester and also want to set the field with newly assigned computer.

there is existing script include as well as catalog client script  but when I used array it throws the error org.mozilla.javascript.NativeArray 

Please suggest ,what is going wrong here 

Catalog client script:- 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var ga = new GlideAjax('PL4UserServiceAjax');
    ga.addParam('sysparm_name', 'getUserComputer');
    ga.addParam('sysparm_user', newValue);
    ga.getXMLAnswer(setComputer);
}

function setComputer(response){
    g_form.setValue('Computername', response);

}

Script include :- 

getUserComputer: function(userId) {
        var host =[];
        var comp = new GlideRecord("cmdb_ci_computer");
        comp.addQuery("assigned_to", userId);
    //    comp.orderByDesc("sys_created_on");
        comp.query();
        while (comp.next()) {
            var str = (comp.getValue("name"));
            host.push(str);
        }     
        return host;
    },

Reference Qualifier:-assigned_to=gs.getUserID()^install_status!=7^sys_class_name=cmdb_ci_computer^EQ

Default Value:- javascript: new PL4UserService().getUserComputer(gs.getUserID())

16 REPLIES 16

Nikita30
Mega Sage

Hi @asmita sahu

 

Please update the code

Catalog client script:- 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var ga = new GlideAjax('PL4UserServiceAjax');
    ga.addParam('sysparm_name', 'getUserComputer');
    ga.addParam('sysparm_user', newValue);
    ga.getXML(setComputer);
}

function setComputer(response){
    var answer = response.responseXML.documentElement.getAttribute("answer"); // mandatory line

alert(answer);
    g_form.setValue('Computername', answer); //Please check the computer field name 

}

Script include :- 

getUserComputer: function() {
        var host = [];
        var userId = this.getParameter('sysparm_user');
        var comp = new GlideRecord("cmdb_ci_computer");
        comp.addQuery("assigned_to", userId);
        comp.orderByDesc("sys_created_on");
        comp.query();
        while (comp.next()) {
  var str = comp.getValue("name");
            host.push(str);
        }
        return host.toString();
    },

Please mark the response as Helpful/Correct, if applicable. Thanks!

Community Alums
Not applicable

Hi Nikita,

I tried your code but getting below output that is not expected , computer names are repeating.

Hi Please try ArrayUtil for unique values getUserComputer: function() { var host = []; var userId = this.getParameter('sysparm_user'); var comp = new GlideRecord("cmdb_ci_computer"); comp.addQuery("assigned_to", userId); comp.orderByDesc("sys_created_on"); comp.query(); while (comp.next()) { var str = comp.getValue("name"); host.push(str); } var arrayUtil = new global.ArrayUtil(); host = arrayUtil.unique(host); return host.toString(); },

Community Alums
Not applicable

Hi Nikita ,

I tried but still same and I missed to mention there are 2 script include :-

Client script we are calling below script then from this script other script in which I made the changes and also I want the computer names that are assigned to caller but with this script I am getting all the computer names present in table

getUserComputer: function(){
        var computerID;
        var userId = this.getParameter("sysparm_user");
        var computer = new PL4UserService().getUserComputer(userId);           
   
    },