Make check box variables Mandatory depending on select box choices
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2025 12:09 AM
Hi All,
Thank you for taking your time and helping me out.
Please help me out, I am working for long time on below requirement.
I have a requirement where
We have 6 check box variables(next to each other like options variable):
If choice 1 is selected then all 6 checkboxes should be available for checking but out of particular 3 checkbox variables one should definitely be selected
If choice 2 is selected then all 6 checkboxes should be available out of which 3 should definitely be selected
If choice 3 is selected then 3 check box variables should definitely be selected and out of other 3 check boxes any of one should be selected
ie. For example I have a select box variable SBV with choices A, B, C and the Checkboxes D1, D2, D3, D4, D5, D6
If A is selected we need 1 of (D1, D2, and D3) to be required
If B is selected, we need ALL the (D4, D5, D6) and add a note that says “If B is selected all three choices should definitely be selected”
If Cis selected we need all the (D4, D5, D6) to be required and one of (D1, D2, and D3) is required.
Below is the code that I tried working on:
Onchange of variable SBV
Please help me. Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2025 12:24 AM
try this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Clear previous messages and reset mandatory fields
g_form.clearMessages();
g_form.setMandatory('D1', false);
g_form.setMandatory('D2', false);
g_form.setMandatory('D3', false);
g_form.setMandatory('D4', false);
g_form.setMandatory('D5', false);
g_form.setMandatory('D6', false);
g_form.setVisible('D1', true);
g_form.setVisible('D2', true);
g_form.setVisible('D3', true);
g_form.setVisible('D4', true);
g_form.setVisible('D5', true);
g_form.setVisible('D6', true);
switch (newValue) {
case 'A':
// Make one of D1, D2, D3 required
g_form.setMandatory('D1', true);
g_form.setMandatory('D2', true);
g_form.setMandatory('D3', true);
g_form.addInfoMessage('Please select at least one of D1, D2, or D3.');
break;
case 'B':
// Make D4, D5, D6 required
g_form.setMandatory('D4', true);
g_form.setMandatory('D5', true);
g_form.setMandatory('D6', true);
g_form.addInfoMessage('If B is selected, all three choices (D4, D5, D6) should definitely be selected.');
break;
case 'C':
// Make D4, D5, D6 required and one of D1, D2, D3 required
g_form.setMandatory('D4', true);
g_form.setMandatory('D5', true);
g_form.setMandatory('D6', true);
g_form.setMandatory('D1', true);
g_form.setMandatory('D2', true);
g_form.setMandatory('D3', true);
g_form.addInfoMessage('If C is selected, all three choices (D4, D5, D6) should be selected and one of D1, D2, or D3 should be selected.');
break;
default:
break;
}
}
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
01-23-2025 12:55 AM
No, here functionality is missing. will you be able to please check the other response provided by Rajesh and the issues that I am facing and help me out.
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2025 01:12 AM
Please share what script you are using now and what's the challenge
you can always enhance the code.
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
01-23-2025 01:16 AM