Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Getting the value as object object

JPSS
Tera Contributor
 
 
Client Script
function onLoad() {
     
    var sap = new GlideAjax('ProcessTraining');
    sap.addParam('sysparm_name','closecategory');
    sap.getXML(categorycallback)



   function categorycallback(answer) {
    g_form.addInfoMessage(answer);
        answer = JSON.parse(answer); // parse answer
        alert("Answer: " + answer);
        tlist=answer.split(",");
   
}

 

}//Type appropriate comment here, and begin script below
 
 
Script Include
var ProcessTraining = Class.create();
ProcessTraining.prototype = Object.extendsObject(AbstractAjaxProcessor, {
       
    closecategory: function(){
        var category_arr=[];
            var category=new GlideRecord("u_choice_list");
     category.addEncodedQuery("u_type=service_catalog^u_parent.u_parent.u_choice_value=Tech^u_parent.u_choice_value=HR");
            category.query();
        while(category.next()){
            category_arr.push(category.sys_id);
   
        }
        gs.addInfoMessage(category_arr.toString());
    return category_arr.toString();
       
    },
    type: 'ProcessTraining'
});
 
Coud you please help on this. i am getting the answer as [object][object]
   



5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

You need to force the u_choice_list sys_id to a string when you push it to the array, or you will get an array of the same sys_id, and it would be best to explicitly join the array on the return so you can control the format:

var ProcessTraining = Class.create();
ProcessTraining.prototype = Object.extendsObject(AbstractAjaxProcessor, {
       
    closecategory: function(){
        var category_arr=[];
        var category=new GlideRecord("u_choice_list");
category.addEncodedQuery("u_type=service_catalog^u_parent.u_parent.u_choice_value=Tech^u_parent.u_choice_value=HR");
        category.query();
        while (category.next()) {
            category_arr.push(category.sys_id.toString());
        }
        gs.addInfoMessage(category_arr.toString());
        return category_arr.join(',');
     },
    type: 'ProcessTraining'});

Since this is a simple array, there's no JSON / parsing, in the client script you would just split the answer if you want an array, and you are missing a key line of code to get the answer from the GlideAjax:

function onLoad() {
    var sap = new GlideAjax('ProcessTraining');
    sap.addParam('sysparm_name','closecategory');
    sap.getXML(categorycallback):

    function categorycallback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.addInfoMessage(answer);
        tlist=answer.split(",");
    }
}