- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 02:36 AM
Dear Community,
I am setting up a catalog UI policy based on a variable (city) and want to set values for the other 5 variables related to it.
I have 50 cities in the city variable and 5 variables to set the values.
If I add it manually, I have to create 50 catalog UI policies and their actions.
Is there a way to do it by UI policy script or any other alternative?
I appreciate if you can share a script to proceed with this action.
Regards,
Burhan
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 02:47 AM
better you use onChange catalog client script with something like this and enhance
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Define the city and corresponding values for the other variables
var cityValues = {
"City1": ["Value1_1", "Value1_2", "Value1_3", "Value1_4", "Value1_5"],
"City2": ["Value2_1", "Value2_2", "Value2_3", "Value2_4", "Value2_5"],
// Add more cities and their corresponding values here
};
// Get the selected city
var selectedCity = g_form.getValue('city_variable');
// Check if the selected city exists in the cityValues object
if (cityValues[selectedCity]) {
// Set the values for the other variables
g_form.setValue('variable1', cityValues[selectedCity][0]);
g_form.setValue('variable2', cityValues[selectedCity][1]);
g_form.setValue('variable3', cityValues[selectedCity][2]);
g_form.setValue('variable4', cityValues[selectedCity][3]);
g_form.setValue('variable5', cityValues[selectedCity][4]);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 02:46 AM - edited 03-05-2025 02:48 AM
Hi @Burhan Shah
In this you can create a only one OnChange Client Script on Variable City and in If condition use g_form to make read Only, Mandatory or Visible for your five variables. Less time it will take then UI policy.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
if(newValue == "Delhi")
{
g_form.setMandatory("variable_name",true);
g_form.setVisible("variable_name_1",true);
g_form.setReadOnly("variable_name_3",true);
}
if(newValue == "Pune")
{
g_form.setMandatory("variable_name",false);
g_form.setVisible("variable_name_1",false);
g_form.setReadOnly("variable_name_3",true);
}
}
If my response helped, please mark it helpful and accept the solution so that it benefits future readers.
Regards,
Rohit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 02:47 AM
better you use onChange catalog client script with something like this and enhance
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Define the city and corresponding values for the other variables
var cityValues = {
"City1": ["Value1_1", "Value1_2", "Value1_3", "Value1_4", "Value1_5"],
"City2": ["Value2_1", "Value2_2", "Value2_3", "Value2_4", "Value2_5"],
// Add more cities and their corresponding values here
};
// Get the selected city
var selectedCity = g_form.getValue('city_variable');
// Check if the selected city exists in the cityValues object
if (cityValues[selectedCity]) {
// Set the values for the other variables
g_form.setValue('variable1', cityValues[selectedCity][0]);
g_form.setValue('variable2', cityValues[selectedCity][1]);
g_form.setValue('variable3', cityValues[selectedCity][2]);
g_form.setValue('variable4', cityValues[selectedCity][3]);
g_form.setValue('variable5', cityValues[selectedCity][4]);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 03:28 AM
A bit confused with the client scripts shared by @Ankur Bawiskar & @Rohit Singh
Gentlemen,
I am attaching an example here; will it cover the purpose with your provided scripts?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 03:30 AM
Please don't use UI policy for this requirement.
You can use client script I shared and enhance it to show the variables as well.
The script I shared will just set the value.
Can you please share what confusion is there?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader