ServiceNow Catalog item Client Script & Ui Policy conflicts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
If there are variables like "Region" & "Unit". Region is a Multiple choice type variable & Unit is a select box type of variable now based on the region & unit selection there is other variables which are 11 checkbox variables which should be mandatory & visible based on the selection of Region & unit & if its not selected then these checkboxes should not be visible on the catalog form. For this the On change of unit is already been implemented & tried through UI Policy also but the checkboxes after the selection of unit & region & then deselect those options. Clear Value script also implemented. if anybody is having some suggestions they are most welcomed. Thanks!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Use a Catalog Client Script with proper state management that handles both visibility AND value clearing in a single, coordinated script.
Step 1: Remove Existing Implementations
- Deactivate any UI Policies controlling these checkboxes (don't delete - just deactivate for rollback)
- Deactivate existing onChange client scripts for Unit variable
- This prevents conflicts with the new solution
Step 2: Create the Catalog Client Script
Navigate to Service Catalog > Catalog Client Scripts and create a new script:
Configuration:
- Name: Region Unit Checkbox Control
- UI Type: Desktop (or All for multi-device support)
- Type: onChange
- Applies on Catalog Item: [Select your catalog item]
- Variable Set: [Select if using variable set, otherwise leave blank]
- Active: Checked
Variables field - Add both variables that trigger the logic:
- region
- unit
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || isTemplate) {
return;
}
// Get the selected region and unit values
var region = g_form.getValue('region');
var unit = g_form.getValue('unit');
// Define your checkbox variables
var checkboxVars = [
'checkbox_1',
'checkbox_2',
'checkbox_3',
'checkbox_4',
'checkbox_5',
'checkbox_6',
'checkbox_7',
'checkbox_8',
'checkbox_9',
'checkbox_10',
'checkbox_11'
];
// Define the logic for when checkboxes should be visible and mandatory
// Modify this condition based on your specific requirements
var shouldShowCheckboxes = false;
// Example logic - adjust based on your requirements:
// Show checkboxes if specific region AND unit combinations are selected
if (region && unit) {
// Option 1: Show for ANY region and unit combination
shouldShowCheckboxes = true;
// Option 2: Show only for specific combinations (uncomment and modify)
/*
var validCombinations = {
'region_value_1': ['unit_value_1', 'unit_value_2'],
'region_value_2': ['unit_value_3', 'unit_value_4']
};
if (validCombinations[region] && validCombinations[region].indexOf(unit) > -1) {
shouldShowCheckboxes = true;
}
*/
}
// Apply visibility and mandatory settings to all checkboxes
for (var i = 0; i < checkboxVars.length; i++) {
var varName = checkboxVars[i];
if (shouldShowCheckboxes) {
// Show and make mandatory
g_form.setVisible(varName, true);
g_form.setMandatory(varName, true);
} else {
// Hide, remove mandatory, and CLEAR the value
g_form.setVisible(varName, false);
g_form.setMandatory(varName, false);
g_form.setValue(varName, 'false'); // Critical: Clear checkbox value
}
}
}
Step 3: Handle Form Load State
Create a second Catalog Client Script to set initial state on form load:
Configuration:
- Name: Region Unit Checkbox Initial State
- UI Type: Desktop
- Type: onLoad
- Applies on Catalog Item: [Same catalog item]
- Active: Checked
Script:
function onLoad() {
// Define your checkbox variables (same list)
var checkboxVars = [
'checkbox_1',
'checkbox_2',
'checkbox_3',
'checkbox_4',
'checkbox_5',
'checkbox_6',
'checkbox_7',
'checkbox_8',
'checkbox_9',
'checkbox_10',
'checkbox_11'
];
// Get current values
var region = g_form.getValue('region');
var unit = g_form.getValue('unit');
// Determine if checkboxes should be shown
var shouldShowCheckboxes = (region && unit);
// Set initial state
for (var i = 0; i < checkboxVars.length; i++) {
var varName = checkboxVars[i];
if (!shouldShowCheckboxes) {
g_form.setVisible(varName, false);
g_form.setMandatory(varName, false);
g_form.setValue(varName, 'false');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Better set visibility in client script else manage UI policies(especially clear value) with proper order.