The CreatorCon Call for Content is officially open! Get started here.

Auto select or read only

Ramesh_143
Giga Guru

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.? 

5 ACCEPTED SOLUTIONS

Dr Atul G- LNG
Tera Patron
Tera Patron

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. 

 

LearnNGrowAtul_0-1707473183243.png

 

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]

****************************************************************************************************************

View solution in original post

Aman Kumar S
Kilo Patron

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 .

AmanKumarS_0-1707473585154.png

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.

Best Regards
Aman Kumar

View solution in original post

brahmandlapally
Giga Guru

(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);

View solution in original post

VishaalRanS
Tera Guru

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.

 

View solution in original post

6 REPLIES 6

VishaalRanS
Tera Guru

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.