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

Valmik Patil1
Kilo Sage

Hello @JPSS ,

 

In your script include return statement should be as below

 return JSON.stringify(category_arr); 

 

Thanks,

Valmik Patil

still getting the same error

Hello @JPSS ,

 

Instead of getXML  could you try getXMLAnswer

And Try to print answer as

alert(answer[0]);

 

Thanks,

Valmik Patil

Juhi Poddar
Kilo Patron

Hello @JPSS 

  • The issue is in client script "sap.getXML(categorycallback)".
  • Also add try catch to handle the error.
  • Please try the below updated client script and script include.

Updated client script: 

function onLoad() {
    var sap = new GlideAjax('ProcessTraining');
    sap.addParam('sysparm_name', 'closecategory');
    sap.getXMLAnswer(categoryCallback);

    function categoryCallback(answer) {
        try {
            var response = answer.split(","); // Or JSON.parse(answer) if returning JSON format
            g_form.addInfoMessage("Categories: " + response.join(", "));
        } catch (e) {
            console.error("Error parsing response: ", e);
            g_form.addErrorMessage("Failed to retrieve categories.");
        }
    }
}

Script include:

var ProcessTraining = Class.create();
ProcessTraining.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    closecategory: function() {
        var category_arr = [];
        var category = new GlideRecord("u_choice_list");
        
        // Adjust query to target correct criteria
        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()) {
            // Push the sys_id as a string into the array
            category_arr.push(category.sys_id.toString());
        }
        
        // Return the array as a comma-separated string (or JSON format if needed)
        return category_arr.toString(); // or JSON.stringify(category_arr) for JSON format
    },

    type: 'ProcessTraining'
});

I hope this resolves your query!

 

"If you found my answer helpful, please give it a like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar