[object Object] from Script Include to Client Script

tsoct
Tera Guru

Hi,

I am getting [object Object] from client script. Which part went wrong?

 

Script include:

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

    GetCategory: function() {
        var category = {};
        var service_offering = this.getParameter('sysparm_serviceOffering');
        var business_service = this.getParameter('sysparm_businessServices');

        if (business_service) {
            if (business_service == 'gaa7872a1tyys6505e9542ede54bcbc9') { //IT Services
                var choice = new GlideRecord('sys_choice');
                choice.addQuery('dependent_value', service_offering);
                choice.addQuery('element', 'category');
                choice.addQuery('inactive', false);
                choice.query();

                while (choice.next()) {
                    category.push(choice.label);
                }

            } else {
                var choice1 = new GlideRecord('sys_choice');
                choice1.addQuery('dependent_value', business_service);
                choice1.addQuery('element', 'category');
                choice1.addQuery('inactive', false);
                choice1.query();

                if (choice1.next()) {
                    category.push(choice1.label);
                }
            }
            var json = new JSON();
            var category_list = json.encode(category);
			return category_list
        }
    },

    type: 'RITMCategorization'
});

Client script:

function onLoad() {

    var service_offering = g_form.getValue('service_offering');
    var business_service = g_form.getValue('business_service');

    var ga = new GlideAjax('RITMCategorization');
    ga.addParam('sysparm_name', 'GetCategory');
    ga.addParam('sysparm_serviceOffering', service_offering);
    ga.addParam('sysparm_businessServices', business_service);
    ga.getXML(RelCategory);

    function RelCategory(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var answer2 = answer.evalJSON();
        g_form.setValue('u_category', answer2);
       
}
1 ACCEPTED SOLUTION

Hello

 

 

  • Ensure u_category is a choice field: Go to the dictionary definition of u_category and make sure its type is set to Choice.

  • Use g_form.addOption(): Instead of setting the value of the field directly as a string, dynamically populate the drop-down using g_form.addOption().

    update cs :-

  • function onLoad() {
    var service_offering = g_form.getValue('service_offering');
    var business_service = g_form.getValue('business_service');

    var ga = new GlideAjax('RITMCategorization');
    ga.addParam('sysparm_name', 'GetCategory');
    ga.addParam('sysparm_serviceOffering', service_offering);
    ga.addParam('sysparm_businessServices', business_service);

    ga.getXML(function(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");

    if (answer) {
    var categories = JSON.parse(answer);

    // Clear the options from the u_category field
    g_form.clearOptions('u_category');

    // Add a default empty option
    g_form.addOption('u_category', '', '--None--');

    // Loop through the categories and add them as options in the drop-down
    categories.forEach(function(category) {
    g_form.addOption('u_category', category, category);
    });
    }
    });
    }

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

8 REPLIES 8

Harsh_Deep
Giga Sage
Giga Sage

Hello @tsoct 

 

Use this 

 var category = [];

as a array not as a JSON then in place of this 

            var json = new JSON();
            var category_list = json.encode(category);
			return category_list

 

use this

return category_list;

and use below in client script

function RelCategory(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('u_category', answer);
       
}

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

Hello @Harsh_Deep ,

 

The value now return answer [{},{},{},{},{},{},{},{},{}]

 

Omkar Mone
Mega Sage

Hello

 

Can you make these changes,

 

SI - 

 

return new JSON().encode(category);

 

client script

var answer = response.responseXML.documentElement.getAttribute("answer");
var categoryList = JSON.parse(answer);
var categoryString = categoryList.join(', ');
g_form.setValue('u_category', categoryString);

 

Let me know if that works

Amit Verma
Kilo Patron
Kilo Patron

Hi @tsoct 

 

Please try with below code. For the client script, can you please let me know the field type for u_category field. Is it a reference field ? Based on that, we can further refine the client script.

 

Script Include :

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

    GetCategory: function() {
        var category = {};
        var service_offering = this.getParameter('sysparm_serviceOffering');
        var business_service = this.getParameter('sysparm_businessServices');

        if (business_service) {
            if (business_service == 'gaa7872a1tyys6505e9542ede54bcbc9') { //IT Services
                var choice = new GlideRecord('sys_choice');
                choice.addQuery('dependent_value', service_offering);
                choice.addQuery('element', 'category');
                choice.addQuery('inactive', false);
                choice.query();

                while (choice.next()) {
                    category.push(choice.label);
                }

            } else {
                var choice1 = new GlideRecord('sys_choice');
                choice1.addQuery('dependent_value', business_service);
                choice1.addQuery('element', 'category');
                choice1.addQuery('inactive', false);
                choice1.query();

                if (choice1.next()) {
                    category.push(choice1.label);
                }
            }
       
			return category.toString();
        }
    },

    type: 'RITMCategorization'
});

 

Client Script  -

function onLoad() {

    var service_offering = g_form.getValue('service_offering');
    var business_service = g_form.getValue('business_service');

    var ga = new GlideAjax('RITMCategorization');
    ga.addParam('sysparm_name', 'GetCategory');
    ga.addParam('sysparm_serviceOffering', service_offering);
    ga.addParam('sysparm_businessServices', business_service);
    ga.getXML(RelCategory);

    function RelCategory(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('u_category', answer);
       
}

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.