[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);
    });
    }
    });
    }

 

View solution in original post

8 REPLIES 8

Ravi Gaurav
Giga Sage

Hi @tsoct 

where you're getting [object Object] from the client script, suggests that you are attempting to directly display or set an object instead of its string representation or specific value.


Solution:

Updated Script Include:

  1. Change the category variable to an array since you're collecting multiple labels.
  2. Ensure that the JSON is returned correctly.

    var RITMCategorization = Class.create();
    RITMCategorization.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    GetCategory: function() {
    var category = []; // Change to array
    var service_offering = this.getParameter('sysparm_serviceOffering');
    var business_service = this.getParameter('sysparm_businessServices');

    if (business_service) {
    var choice = new GlideRecord('sys_choice');

    if (business_service == 'gaa7872a1tyys6505e9542ede54bcbc9') {
    // IT Services
    choice.addQuery('dependent_value', service_offering);
    } else {
    choice.addQuery('dependent_value', business_service);
    }

    choice.addQuery('element', 'category');
    choice.addQuery('inactive', false);
    choice.query();

    while (choice.next()) {
    category.push(choice.label.toString()); // Ensure the value is a string
    }
    }

    var json = new JSON();
    var category_list = json.encode(category); // Convert array to JSON string
    return category_list; // Return JSON string
    },
    type: 'RITMCategorization'
    });


    Updated Client Script:

    1. Parse the JSON properly using JSON.parse().
    2. Handle the value from answer2 correctly in g_form.setValue.

       

       

      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) {
      // Parse the JSON string into an array
      var answer2 = JSON.parse(answer);

      // Assuming you want to set the first item in the array or join them
      if (Array.isArray(answer2) && answer2.length > 0) {
      g_form.setValue('u_category', answer2.join(', ')); // Join the array values as a comma-separated string
      } else {
      g_form.setValue('u_category', ''); // Default to empty if no categories found
      }
      }
      });
      }

Thank you @Ravi Gaurav 

 

the u_category field is a 'String' but has a drop-down value. I used this script, and the return value is displayed in a long string like label1,label2,label3,label4 instead of

label1

label2

label3

label4

 

How can i make it display in drop down?

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);
    });
    }
    });
    }

 

Not applicable

Hi @tsoct ,

Try using json.stringify(category) , and then return it in the script include.

and use json.parse(answer) before setting the value in 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);
                }
            }
            var json = new JSON();
           //================================================
            var category_list = json.stringify(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 = JSON.parse(answer);
       //===================================================
        g_form.setValue('u_category', answer2);
       
}

 

Thanks

HRISHABH KUMAR

hope that helps.

Mark this as helpful and accept solution is it worked.