Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

10 REPLIES 10

Hi @Juhi Poddar 

 

i facing multirow related one issue not able to solve please do needful  see the steps below

1. I have catalog form Desktop  -> i have multi single variables  and one list collator variable  type that is work_package

2. Same catalog i have multi row variable set 'selected_software_name_details ' in this MVRS i have 3 fields  one is text field <work_package_names> and 2 check boxes 1. <Installation_completed> , 2. <varification_completed>

its displaying row and columms

3. List collator <work_package> have sys_choices (from business application custom field) using script included i have bring the data in this list collator    --- > No issue here 

4. Whatever i have selected work_package names that too displaying one by one row in text field of multi row variable and opposite check boxes also displayed with 'false' this is default values of check boxes in multi row variable set.    ---- upto here working as expected 

5. This Multi row variable set added only TASK2   here this MVRS displaying whatever i have submitted the record but check boxes displaying value with 'False' not showing TRUE whatever i have edited row and saved in MVRS.

 

I am stagging lot not able to solve this issue PDI also i am trying with simple logic but no use

 

if you know anyone help me in advance thank you 

 

Regards,

PB