- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 10:39 PM
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);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 12:51 AM
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);
});
}
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 11:24 PM
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:
- Change the
categoryvariable to an array since you're collecting multiple labels. - 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:
- Parse the JSON properly using
JSON.parse(). - Handle the value from
answer2correctly ing_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
}
}
});
}
- Parse the JSON properly using
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 12:44 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 12:51 AM
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);
});
}
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 11:31 PM
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.