in servicenow, i created a variable set and inside the variable set i created 22 checkbox type variables and i want that if any user check any of the variable then other remaining 21 variables gets read only and non editable, if uncheck then editable if any other variable selected then remaining gets read only again.
Assistant
User
any other method instead of creating 22 client script,
Assistant
User
i thing ui policy and script combo is best
Assistant
User
what to write in function onCondition() ,
my condition is variable 1 is true or variable 2 is true or variable 3 is true, and script like if variable 1 is true then make other readonly and if variable 2 is true
Assistant
User
please listen carefully, is it possible to create ui policy on condition and if false script, on all 22 checkboxes
Assistant
To achieve this behavior in ServiceNow where selecting any checkbox in a Variable Set makes the other checkboxes read-only, and unchecking it re-enables the other checkboxes, you will need to write some client-side script.
How this works:
When the user checks any checkbox variable, the script will make all other checkboxes read-only.
When none are checked, all checkboxes will be editable.
If another checkbox is checked, the same logic applies.
Step-by-step solution:
Identify the variable names in the variable set:
Assuming your variable set name is my_checkbox_set and inside you have variables named:
var1
var2
...
var22
Make sure you have the exact variable names; you will need them in the script.
Add a Catalog Client Script of type onChange to each checkbox variable
We will create a single onChange client script and attach it to each of those 22 variables.
Client Script code
Create a Catalog Client Script. For scope, Global or your app as appropriate.
Type: onChange
Applies to each checkbox variable (you can add the same client script to each checkbox field)
Variable name: the checkbox variable name (you'll repeat this script with each checkbox variable name set in the onChange script)
Script:
javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading)
return;
// List all checkbox variable names in the variable set
var allVariables = ['var1', 'var2', 'var3', 'var4', 'var5', 'var6', 'var7', 'var8', 'var9', 'var10',
'var11', 'var12', 'var13', 'var14', 'var15', 'var16', 'var17', 'var18', 'var19', 'var20',
'var21', 'var22'];
// Check if any checkbox is selected/checked
var anyChecked = false;
for (var i = 0; i < allVariables.length; i++) {
var checked = g_form.getValue(allVariables[i]);
if (checked == 'true' || checked == true || checked == 1 || checked == '1') {
anyChecked = true;
break;
}
}
if (anyChecked) {
// Disable all other variables except the one(s) that are checked
for (var i = 0; i < allVariables.length; i++) {
var val = g_form.getValue(allVariables[i]);
if (val == 'true' || val == true || val == 1 || val == '1') {
// Checked variables stay enabled (editable)
g_form.setReadOnly(allVariables[i], false);
} else {
// Unchecked variables become read-only
g_form.setReadOnly(allVariables[i], true);
}
}
} else {
// None checked => make all editable
for (var i = 0; i < allVariables.length; i++) {
g_form.setReadOnly(allVariables[i], false);
}
}
}
Explanation:
The script first checks if the form is loading (isLoading) and does nothing in that case.
It iterates over all variables, checking if at least one checkbox is checked (i.e., has value true/1).
If any checkbox variable is checked:
It disables (sets read-only) all variables except the checked ones.
If none are checked, it enables (sets read-only = false) all variables.
Notes:
Replace the variable names var1, var2, ... with the actual variable names in your variable set.
You need to add the same client script to all the checkbox variables that are part of your variable set.
Alternatively, create a single Catalog Client Script of type UI Policy with this logic if possible, but usually onChange in this scenario is best.
Optional: Using UI Policy instead of client script
It might be possible with UI policies, but since changes on one checkbox affect all others dynamically, scripting is more flexible.
Let me know if you want help creating the client script tied to multiple variables or for additional scenarios!
Yes! Instead of creating 22 separate onChange client scripts (one for each checkbox), you can achieve this with one single Catalog Client Script of type onLoad, which attaches event handlers dynamically to all checkbox variables in your variable set. This way, the logic applies whenever any checkbox changes, without needing multiple scripts.
Solution: One onLoad Catalog Client Script with dynamic onchange handlers
Step 1: Create a single Catalog Client Script
Type: onLoad
Applies to your catalog item (or catalog task, request depending where your variable set is)
Script:
javascript
function onLoad() {
// List all checkbox variable names in your variable set
var allVariables = [
'var1', 'var2', 'var3', 'var4', 'var5', 'var6', 'var7', 'var8', 'var9', 'var10',
'var11', 'var12', 'var13', 'var14', 'var15', 'var16', 'var17', 'var18', 'var19', 'var20',
'var21', 'var22'
];
// Function to update readonly states based on checkboxes selected
function updateReadOnlyStates() {
var anyChecked = allVariables.some(function(variable) {
var val = g_form.getValue(variable);
return val == 'true' || val == true || val == '1' || val == 1;
});
if (anyChecked) {
allVariables.forEach(function(variable) {
var val = g_form.getValue(variable);
// If checked, editable, else read-only
g_form.setReadOnly(variable, !(val == 'true' || val == true || val == '1' || val == 1));
});
} else {
// None checked: all editable
allVariables.forEach(function(variable) {
g_form.setReadOnly(variable, false);
});
}
}
// Attach onchange handlers for each checkbox variable that calls updateReadOnlyStates
allVariables.forEach(function(variable) {
g_form.getGlideUIElement(variable).on('change', function() {
updateReadOnlyStates();
});
});
// Initialize state on form load
updateReadOnlyStates();
}
Explanation:
This script runs once when the catalog item loads.
It defines the list of checkbox variables.
Defines a reusable function updateReadOnlyStates() that:
Checks if any checkbox is checked.
Sets all others read-only or editable accordingly.
It adds a dynamic onchange event handler on all your checkboxes in the variable set.
It calls the function to initialize the states when the form loads.
Benefits of this approach:
Only one Catalog Client Script, no need to add onChange scripts to each checkbox.
More maintainable and scalable, if you add/remove checkboxes just change the array.
Uses modern event binding on Glide UI Elements, reliable on ServiceNow forms.
Important:
Make sure your variable names in the allVariables array exactly match your checkbox variable names.
Test the script.
This script assumes you are editing catalog variables on a catalog item or record producer form.
If you want, I can also help with a UI Policy + Script combo or other approaches, but this single onLoad Catalog Client Script is usually the cleanest way to handle this in ServiceNow.
Let me know if you want me to generate the exact script with your variable names!
Great! Using a UI Policy + Script combo can also be a neat solution without needing multiple client scripts, and you keep it maintainable and declarative as much as possible.
How to implement using UI Policy + UI Policy Script
Objective Recap:
If any checkbox variable in variable set is checked → all other checkboxes become read-only
If none checked → all checkboxes become editable
Step 1: Create one UI Policy that applies to your variable set
Table:sc_catalog_item or sc_cat_item_option (the table your variables belong to when filling the catalog item form)
Condition: Leave blank so it always applies (or add conditions if needed)
Run scripts: Checked (this enables the UI Policy script)
Reverse if false: No (optional, since we handle in script)
Active: Yes
Step 2: Add a UI Policy Script
In the UI Policy Script field, add the following code:
javascript
// List all checkbox variable names in the variable set
var allVariables = [
'var1', 'var2', 'var3', 'var4', 'var5', 'var6', 'var7', 'var8', 'var9', 'var10',
'var11', 'var12', 'var13', 'var14', 'var15', 'var16', 'var17', 'var18', 'var19', 'var20',
'var21', 'var22'
];
// Function to check if any checkbox is checked
function isAnyChecked() {
for (var i = 0; i < allVariables.length; i++) {
var val = g_form.getValue(allVariables[i]);
if (val == 'true' || val == true || val == '1' || val == 1)
return true;
}
return false;
}
var anyChecked = isAnyChecked();
for (var i = 0; i < allVariables.length; i++) {
var variable = allVariables[i];
var checked = g_form.getValue(variable);
if (anyChecked) {
// If any is checked, make unchecked ones read-only, checked ones editable
if (checked == 'true' || checked == true || checked == '1' || checked == 1)
g_form.setReadOnly(variable, false);
else
g_form.setReadOnly(variable, true);
} else {
// If none checked, all editable
g_form.setReadOnly(variable, false);
}
}
Step 3: Trigger the UI Policy on checkbox change events
By default, UI Policies run onLoad and onChange of fields they apply to.
To make sure this UI Policy runs on all checkboxes, map all checkbox variables to this UI Policy.
How?
In the related list "UI Policy Actions" add an action for each checkbox variable:
Variable: (one checkbox variable)
Read-only: leave blank (script controls it)
Mandatory: No
Visible: leave blank
Repeat adding UI policy actions to all 22 variables.
Thus, UI Policy gets triggered on change of any checkbox variable.
Alternative: Using Catalog Client Script to trigger UI Policy (if needed)
If UI Policy does not trigger automatically on all checkbox changes, add this client script to fire the UI policy:
javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading)
return;
// Runs UI policy manually
g_form.refreshUI();
}
But usually, UI Policies refresh automatically on field changes mapped via UI policy actions.
Summary:
One UI Policy covers the logic.
One UI Policy script controls read-only state of the checkboxes dynamically.
UI Policy Actions let you trigger the policy on changes of your variables.
No need to create 22 client scripts, just 1 UI policy + actions + script.
If you want me to write the exact steps in the UI or additional helper scripts, just ask!
When using a UI Policy in ServiceNow and specifying a condition (the "Condition" field in the UI Policy form), the onCondition() function is auto-generated by ServiceNow based on that condition — you usually don't need to write it manually.
What is onCondition()?
It’s the function ServiceNow generates behind the scenes.
It evaluates your UI Policy condition.
Returns true or false to determine if UI Policy Actions run.
Your case: Condition
variable1 is true OR variable2 is true OR variable3 is true
How to create the condition:
In your UI Policy Condition builder, you can add multiple OR conditions like:
[variable1] [is] [true]
OR
[variable2] [is] [true]
OR
[variable3] [is] [true]
Then, your actual logic for making variables readonly based on which checkbox is selected should be in the UI Policy Script, NOT in onCondition().
Example setup
1. UI Policy Condition
code
variable1 is true OR variable2 is true OR variable3 is true
2. UI Policy Script
Put your logic in the UI Policy Script field:
javascript
// List of your checkbox variable names
var variables = ['variable1', 'variable2', 'variable3']; // add rest if needed
function isChecked(variable) {
var val = g_form.getValue(variable);
return val === 'true' || val === true || val === '1' || val === 1;
}
// Check which variable(s) is/are checked
var checkedVars = variables.filter(function(v) {
return isChecked(v);
});
if (checkedVars.length > 0) {
// For each variable:
variables.forEach(function(v) {
if (checkedVars.indexOf(v) !== -1) {
// Keep checked variable(s) editable
g_form.setReadOnly(v, false);
} else {
// Make unchecked variables read-only
g_form.setReadOnly(v, true);
}
});
} else {
// If none selected, make all variables editable
variables.forEach(function(v) {
g_form.setReadOnly(v, false);
});
}
Summary
onCondition() is automatically generated by ServiceNow based on the Condition you enter in UI Policy form; you don’t write this manually.
Write your custom logic in the UI Policy Script box.
Use the UI Policy condition to trigger your policy only when any checkbox is checked.
If you want to not use any condition at all, you can leave the condition blank and handle everything inside your script by checking variables’ values.
If you want me to generate the full UI Policy form setup or explain how to attach UI Policy actions to variables, just let me know!
Yes, absolutely — you can create one UI Policy with a condition that applies to all 22 checkboxes, and then use the UI Policy script to handle both the true and false cases to set read-only states dynamically on all those checkboxes.
Key points:
UI Policy condition is evaluated once.
If true, UI Policy runs the script with the condition true.
If false, the UI Policy runs the script but the condition is false.
If you want logic to run regardless of condition (true or false), then you must put in script and check the condition yourself or leave condition blank and handle in script.
Can one UI Policy control 22 checkboxes?
Yes, by:
Adding UI Policy Actions for all 22 checkbox variables (each action on one variable).
These actions define which variables trigger the policy and which variables are affected.
Then your UI Policy Script manages the read-only logic on those variables.
How to do what you want:
Step 1: Create the UI Policy
Table: (your catalog form table, e.g. sc_cat_item_option or the Record Producer table)
Condition: For example, your condition can be:
variable1 is true OR variable2 is true OR ... OR variable22 is true
(or leave blank to run every time)
Run scripts: checked
Reverse if false: unchecked (because you will manually handle false case in script)
Active: checked
Step 2: Add 22 UI Policy Actions
Add a UI Policy Action for each checkbox var to target it.
Adding these actions ensures the UI Policy triggers on change of any checkbox variable.
Step 3: Write the UI Policy Script
Here you handle both cases based on the condition:
javascript
// Variables list
var allVars = [
'variable1', 'variable2', 'variable3', 'variable4', 'variable5',
'variable6', 'variable7', 'variable8', 'variable9', 'variable10',
'variable11', 'variable12', 'variable13', 'variable14', 'variable15',
'variable16', 'variable17', 'variable18', 'variable19', 'variable20',
'variable21', 'variable22'
];
// Helper to check if a variable is checked
function isChecked(varName) {
var val = g_form.getValue(varName);
return val === 'true' || val === true || val === '1' || val === 1;
}
// Check if condition is true (at least one checked)
var anyChecked = allVars.some(isChecked);
if (anyChecked) {
// When condition is true: make unchecked variables read-only, checked editable
allVars.forEach(function(v) {
if (isChecked(v))
g_form.setReadOnly(v, false);
else
g_form.setReadOnly(v, true);
});
} else {
// When condition is false: make all variables editable
allVars.forEach(function(v) {
g_form.setReadOnly(v, false);
});
}
Explanation:
The boolean anyChecked matches your condition.
The script enforces read-only or editable states accordingly.
This works whether UI Policy condition evaluates as true or false.
You get centralized control in one UI Policy.
Important notes:
UI Policy condition can't easily handle complex logic for 22 variables (or can, but is cumbersome).
You can leave UI Policy condition blank and handle everything in the script (checking variable values yourself). Then policy always runs whenever any variable changes.
Make sure you add UI Policy Actions for all 22 checkbox variables so the policy triggers on their changes.
Summary:
Step
Action
1. Create UI Policy
With condition: OR of all 22 variables or empty (to run always)
2. Add UI Policy Actions
Add actions for all 22 checkboxes
3. Use UI Policy Script
Script checks which checkboxes selected and sets readonly
If you want, I can help draft the entire UI Policy config for you including condition and actions!