- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 08:39 AM - edited 11-07-2024 08:43 AM
I have a multi-row variable set where each row has a checkbox.
It's possible to have multiple checkboxes selected (for example, 5 rows with 5 checkboxes selected).
How can I make sure that, when the form is submitted, no more than one checkbox is selected, and if more than one is selected, display an error message to the user?
Thanks
Matt
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 09:40 AM
Here's one way to check onSubmit of the request form, alerting and preventing submit if more than one row has the box checked.
function onSubmit() {
var checked = 'false';
var mrvs = JSON.parse(g_form.getValue('configuration_item_mrvs')); //MRVS internal name
for (var i = 0; i < mrvs.length; i++) {
if (mrvs[i].v_primary_mrvs == 'true') { //MRVS variable name
if (checked == 'false') {
checked = 'true';
} else {
alert ('More than one row has a box checked');
return false;
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 10:02 AM
Hello @Matt25
To enforce that only one checkbox is selected in a Multi-Row Variable Set (MRVS), you can add an onSubmit client script to the catalog item. This script counts the selected checkboxes when the form is submitted and shows an error message if more than one is selected, preventing submission.
Here's the script:
function onSubmit() {
var selectedCount = 0;
var mrvsRows = g_form.getValue('cmdb_ci'); //Mrvs variable set internal name
if (mrvsRows) {
var rows = JSON.parse(mrvsRows);
rows.forEach(function(row) {
if (row.check == 'true') { //here check is the checkbox variable name
selectedCount++;
}
});
}
if (selectedCount > 1) {
g_form.addErrorMessage("Only one checkbox can be selected. Please review your selections.");
return false; // Prevent form submission
}
return true; // Allow form submission
}
I validated this solution in my Personal Developer Instance (PDI) to confirm that it functions as expected. This solution ensures that if multiple checkboxes were initially selected but then corrected to one, the form will pass validation.
"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 10:02 AM
Hello @Matt25
To enforce that only one checkbox is selected in a Multi-Row Variable Set (MRVS), you can add an onSubmit client script to the catalog item. This script counts the selected checkboxes when the form is submitted and shows an error message if more than one is selected, preventing submission.
Here's the script:
function onSubmit() {
var selectedCount = 0;
var mrvsRows = g_form.getValue('cmdb_ci'); //Mrvs variable set internal name
if (mrvsRows) {
var rows = JSON.parse(mrvsRows);
rows.forEach(function(row) {
if (row.check == 'true') { //here check is the checkbox variable name
selectedCount++;
}
});
}
if (selectedCount > 1) {
g_form.addErrorMessage("Only one checkbox can be selected. Please review your selections.");
return false; // Prevent form submission
}
return true; // Allow form submission
}
I validated this solution in my Personal Developer Instance (PDI) to confirm that it functions as expected. This solution ensures that if multiple checkboxes were initially selected but then corrected to one, the form will pass validation.
"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 12:05 PM
Thank you JuhiPoddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 03:50 AM
Hi @Juhi Poddar ,
How can you use this script to check each row rather than the entire MVRS.
i.e. To enforce that only one checkbox is selected in EACH ROW of the Multi-Row Variable Set (MRVS)?
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 04:25 AM
Hello @James Rostron
Do you mean you have two checkbox in each row?
The above code checks the variable 'check' is selected in entire mrvs.
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 04:27 AM
Yes exactly. I would like to check at least one checkbox = true for each row before submission.