Help with Catalog Client Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2025 03:03 PM
I need help with a catalog client script. For the ''Operational work type' drop down, whatever that is selected there, I would like the 'Work category' drop down to to change to the same thing. The choices for both the drop down are the same, but the value is different.
Thank you in advance for any help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2025 03:25 PM
Hi @Jen11 ,
So the label and value pairs are same for both select box but records are different in Question Choice [question_choice] table.
Write onChange client script on Operational Work Type field and read the current value using below code
var opWorkType = g_form.getValue('operation_work_type'); // update with actual variable name
and then set the work category with that value using the same value
g_form.setValue('work_category',opWorkType); // update with actual variable name for work category
Please test and let us know if works for you or not.
-Thanks,
AshishKM
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2025 03:31 PM
the label is same, but the value is differnt.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2025 08:25 PM
Hi @Jen11,
Please refer to the screenshot below.
To proceed, go to the Catalog Client Script section and create a new script using the same code provided below.
Let me know if you'd like any further adjustments!
"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!"
Thanks & Regards,
Barath.P
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2025 10:18 PM
Hi @Jen11 ,
This is a scalable code that will also be helpful in the future if any new choices are added or removed.
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// Exit if the form is loading or the new value is empty
if (isLoading || newValue === '') {
return;
}
var choiceValue = g_form.getValue('u_operational_work_type');
var choiceLabel = g_form.getOption('u_operational_work_type', choiceValue).text;
var tableName = g_form.getTableName();
var element = 'u_work_category';
// Create an AJAX request to fetch the label of the category
var ajax = new GlideAjax('global.getLabelOfCategory');
ajax.addParam('sysparm_name', 'getValue');
ajax.addParam('label', choiceLabel);
ajax.addParam('table', tableName);
ajax.addParam('elementName', element);
// Call the callback function once the response is received
ajax.getXML(callMe);
}
function callMe(response) {
// Retrieve the answer from the response and set it in the form
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_work_category', answer);
}
Client Callable Script Include:
var getLabelOfCategory = Class.create();
getLabelOfCategory.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// Method to get the value of the category label
getValue: function() {
var choiceLabel = this.getParameter('label');
var table = this.getParameter('table');
var element = this.getParameter('elementName');
var choiceGr = new GlideRecord('sys_choice');
choiceGr.addQuery('name', table);
choiceGr.addQuery('element', element);
choiceGr.addQuery('label', choiceLabel);
choiceGr.query();
// Return the value if found
if (choiceGr.next()) {
return choiceGr.value.toString();
}
},
type: 'getLabelOfCategory'
});
If this answers your query, please mark it as an accepted solution or helpful!
Best Regards,
Pooja