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

@amogh_b6 

so does it mean every row of MRVS should have variable 2 populated in it?

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

@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

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

 

 

@amogh_b6 

script I shared above should also work fine.

Did you try that?

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