- 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);
});
}
});
}
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/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 10:50 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 10:55 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 11:17 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 11:24 PM
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.