Checkbox in Multi row variable set

Matt25
Giga Guru
Hi,

 

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

2 ACCEPTED SOLUTIONS

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;
			}
		}
	}
}

View solution in original post

Juhi Poddar
Kilo Patron

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

 

View solution in original post

9 REPLIES 9

Brad Bowman
Kilo Patron
Kilo Patron

Hi Matt,

Is this one checkbox variable in the MRVS, or are there multiple?  You can do this onSubmit of the request form, but it might make more sense / be more user-friendly to check for this as each row is added/edited.  You could make the variable read-only when the add/edit row dialog is loaded if a row already has the box checked, or warn/prevent submit of a new/edited row if checking the box when another row is already checked.  Are you using the native UI / Service Catalog, or Service Portal / ESC / ...?

Thanks for replying, it is one checkbox per row, and it is ESC

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;
			}
		}
	}
}

Thanks Brad, worked a treat 🙂