- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 01:43 AM
Hi@everyone, I got requirement that there are two questions, each question has two multiple choices, if we select a choice of 1st question then 2nd question of two choices, one should auto select and a remaining option should be hide or should become read-only, we can't use ui policies how can we do that.?
Solved! Go to Solution.
- 867 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 02:06 AM
Hi @Ramesh_143
Greeting!!
I tried this with UI policy it will not work but if you create variable set for Q1 and Q2 then UI action can be applied as you need.
I tried like this.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 02:14 AM
Hi @Ramesh_143
You can use UI policy if you are in Vancouver, now we have set value option and set read only for the 2nd field .
else you will have to go with onchange client script, using if condition you can check value for field 1 , then set value for field 2 and make it read only.
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2024 11:21 PM
(function executeRule(current, previous /*null when async*/) {
// Ensure this script runs when the form loads or the first question's value changes
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Get the second question field
var secondQuestionField = g_form.getField('question2');
if (!secondQuestionField) {
return;
}
// Define the options for the second question based on the first question's value
var optionsForQuestion2 = {
'choice1': 'option1',
'choice2': 'option2'
};
// Clear existing options from the second question
g_form.clearOptions('question2');
// Add the appropriate option based on the first question's selection
if (newValue == 'choice1') {
g_form.addOption('question2', 'option1', 'Option 1');
g_form.setValue('question2', 'option1'); // Auto-select the option
} else if (newValue == 'choice2') {
g_form.addOption('question2', 'option2', 'Option 2');
g_form.setValue('question2', 'option2'); // Auto-select the option
}
// Make the second question read-only
g_form.setReadOnly('question2', true);
}
// Register the onChange event handler
g_form.getField('question1').on('change', onChange);
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2024 02:10 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2024 02:14 AM
Hi @Ramesh_143
To achieve the requirement of auto-selecting an option in the second question based on the selection made in the first question, while also hiding or making the remaining option read-only without using UI Policies in ServiceNow, you can utilize Client Scripts.
1. Create Two Multiple Choice Variables
2. Client Script Setup with Script Logic
Please refer the below links for details:
Solved: How to add Multi Select Variable in Workflow If co... - ServiceNow Community
Add options based on choices - ServiceNow Community
Thanks, and Regards
Vishaal
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 02:06 AM
Hi @Ramesh_143
Greeting!!
I tried this with UI policy it will not work but if you create variable set for Q1 and Q2 then UI action can be applied as you need.
I tried like this.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 02:10 AM
@Ramesh_143 you can achieve through on change catalog client script on question1( i believe you are trying on portal page catalog item), there you can put if statement on question1 vakue and show whatever you wish for qiestion2.
Hope this will help you

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 02:14 AM
Hi @Ramesh_143
You can use UI policy if you are in Vancouver, now we have set value option and set read only for the 2nd field .
else you will have to go with onchange client script, using if condition you can check value for field 1 , then set value for field 2 and make it read only.
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2024 11:21 PM
(function executeRule(current, previous /*null when async*/) {
// Ensure this script runs when the form loads or the first question's value changes
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Get the second question field
var secondQuestionField = g_form.getField('question2');
if (!secondQuestionField) {
return;
}
// Define the options for the second question based on the first question's value
var optionsForQuestion2 = {
'choice1': 'option1',
'choice2': 'option2'
};
// Clear existing options from the second question
g_form.clearOptions('question2');
// Add the appropriate option based on the first question's selection
if (newValue == 'choice1') {
g_form.addOption('question2', 'option1', 'Option 1');
g_form.setValue('question2', 'option1'); // Auto-select the option
} else if (newValue == 'choice2') {
g_form.addOption('question2', 'option2', 'Option 2');
g_form.setValue('question2', 'option2'); // Auto-select the option
}
// Make the second question read-only
g_form.setReadOnly('question2', true);
}
// Register the onChange event handler
g_form.getField('question1').on('change', onChange);
})(current, previous);