Restrict the submission of a form if a condition is true in Multi-Row variable set

amogh_b6
Tera Expert

Hello ServiceNow developers,

 

In a catalog item, we have a Multi-row variable set named as "MR variable" and in that we have 2 variables: "variable 1" and "variable 2".  We have a requirement to restrict the submission of this form along with displaying an error message, if the "variable 2" field is empty even in one of the sets of data added by the users in this variable set. Could you help me with the client script to meet this requirement?

2 ACCEPTED SOLUTIONS

@amogh_b6 

if yes then do this in onSubmit catalog client script on that catalog item

function onSubmit(){
	var jsonString = g_form.getValue('mrvsVariableSetName'); // give the mrvs variable set name here

	var isEmpty = false;
	var parsedData = JSON.parse(jsonString);

	for(var i=0;i<parsedData.length;i++){
		if(parsedData[i].variable2 == ''){ // give here the variable 2 name
			isEmpty = true;
			break;
		}
		else{
			isEmpty = false;
		}
	}

	if(isEmpty){
		alert("All the rows of MRVS should have variable 2 populated");
		return false;
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

Hi @Ankur Bawiskar , i tried using your solution posted in this question  Solved: Validation on Multi Row Variable Set - ServiceNow Community and it works. I just replaced the variable set name, variable name and changed other things. It prevents the submission and displays the error message as well, when the "variable 2" field is kept empty.

 

 

function onSubmit() {
 
    var values = g_form.getValue('variable set name'); // give here mrvs variable name
 
    var parser = JSON.parse(values);
 
    for (var i = 0; i < parser.length; i++) {
 
        var total = parser[i].variable_2; // give here the variable name for proportion
 
        if (total != "") {
 
            g_form.addErrorMessage('Not allowed to submit');
 
            return false;
 
        }
}
}

 

 

View solution in original post

9 REPLIES 9

@Ankur Bawiskar 

Yes, i also tried the above script posted by you, it also works fine as expected. I have accepted that as solution as well in this article. Thank you so much for the help.

 

Regards,

Amogh.

@Ankur Bawiskar 

What if we wanted to prevent submission of the form based on a single variable? I tried some code, but it did not work. 

function onSubmit() {
   //Type appropriate comment here, and begin script below
   var accomm = g_form.getValue('u_accommodation_type');
    if (accomm == 'religious') {
        g_form.addInfoMessage('Please click here to submit a religious accommodation request.');
		return false;
    }
}

 

Hi Ankur

 

Can we have this on onchange of a variable in MRVS, so that error message will be popped on the variable itself and how can we clear just that variable value, not the entire MRVS?

 

Thanks

 

sharan16
Tera Contributor

@Ankur Bawiskar The script you shared does not work in ServicePortal. Did it work for you in portal?

@sharan16 

can you post a new question and tag me there with all the details as this thread is old?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader