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

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

 

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,

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

Yes exactly. I would like to check at least one checkbox = true for each row before submission.