Prevent Submitting the form if Multi row variable set is empty

reddy8055
Tera Contributor

Hi,

I am populating multi row variable set based on requested for user info and want to prevent submitting the form whenever MRVS is empty. Tried mrvrs == '' like highlighted below in red but its not working.

 

function onSubmit() {
var today = new Date();
var mrvs = g_form.getValue('vta_training_details'); // internal name of your MRVS
alert(mrvs);
var obj = JSON.parse(mrvs);
for (var i = 0; i < obj.length; i++) {
var startDTe = obj[i].course_completed_date; // name of the MRVS variable
//alert(startDTe);
var startDateNum = getDateFromFormat(startDTe, g_user_date_format);
var coursedate = today - startDateNum;
//alert(coursedate);
var dayDifference = coursedate / (1000 * 60 * 60 * 24);
alert(dayDifference);
if ((dayDifference > 365) || (mrvs == '') || (mrvs == null)) {
g_form.addErrorMessage("Course Completed Date should not be more than 365 days");
return false;
}
}

 

Thanks,

11 REPLIES 11

Valmik Patil1
Kilo Sage

Hello, You need to parse MRVS object as below

function onSubmit() {
   //Type appropriate comment here, and begin script below
	var a = g_form.getValue('test_mrvs'); // Name of MRVS
	var b = JSON.parse(a);
	if(b==""){
		alert("empty");
		// add your code here
	}
   
}

Thanks,

Valmik

jaheerhattiwale
Mega Sage
Mega Sage

@reddy8055 Tried and tested solution. You can try following code in your on submit client script:

 

var mrvs = JSON.parse(g_form.getValue('vta_training_details'));

 

if(mrvs.length == 0){

return false;

}

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Hi,

Tried this as well, its not working.

function onSubmit() {
var today = new Date();
var mrvs = g_form.getValue('vta_training_details'); // internal name of your MRVS
var x = g_form.getValue('course_completed_date');
var y = g_form.getValue('name');
var z = g_form.getValue('email');
var total = 0;
// alert(mrvs);
var obj = JSON.parse(mrvs);
for (var i = 0; i < obj.length; i++) {
var startDTe = obj[i].course_completed_date; // name of the MRVS variable
total = total + parseInt(parser[i]).course_completed_date; // give here the variable name for proportion
alert(total);
//alert(startDTe);
var startDateNum = getDateFromFormat(startDTe, g_user_date_format);
var coursedate = today - startDateNum;
//alert(coursedate);
var dayDifference = coursedate / (1000 * 60 * 60 * 24);
alert(dayDifference);
if ((dayDifference > 365) || (mrvs.length == 0)) {
g_form.addErrorMessage("Course Completed Date should not be more than 365 days");
return false;
}

}
}

Hi,

I tried this, its not working..

function onSubmit() {
var today = new Date();
var mrvs = g_form.getValue('vta_training_details'); // internal name of your MRVS
var x = g_form.getValue('course_completed_date');
var y = g_form.getValue('name');
var z = g_form.getValue('email');
var total = 0;
// alert(mrvs);
var obj = JSON.parse(mrvs);
for (var i = 0; i < obj.length; i++) {
var startDTe = obj[i].course_completed_date; // name of the MRVS variable
total = total + parseInt(parser[i]).course_completed_date; // give here the variable name for proportion
alert(total);
//alert(startDTe);
var startDateNum = getDateFromFormat(startDTe, g_user_date_format);
var coursedate = today - startDateNum;
//alert(coursedate);
var dayDifference = coursedate / (1000 * 60 * 60 * 24);
alert(dayDifference);
if ((dayDifference > 365) || (mrvs.length == 0)) {
g_form.addErrorMessage("Course Completed Date should not be more than 365 days");
return false;
}

}
}

@reddy8055 As you are looping over the obj (That is the parsed mvrs). If its length is zero then it will not go inside the loop and you are returning false in for loop. So the code is not reaching the return line.

 

So please update your code as below, this should definitely fix your issue.

 

function onSubmit() {
var today = new Date();
var mrvs = g_form.getValue('vta_training_details'); // internal name of your MRVS
var x = g_form.getValue('course_completed_date');
var y = g_form.getValue('name');
var z = g_form.getValue('email');
var total = 0;
// alert(mrvs);
var obj = JSON.parse(mrvs);
if (obj.length == 0) {
return false;
} else {
for (var i = 0; i < obj.length; i++) {
var startDTe = obj[i].course_completed_date; // name of the MRVS variable
total = total + parseInt(parser[i]).course_completed_date; // give here the variable name for proportion
alert(total);
//alert(startDTe);
var startDateNum = getDateFromFormat(startDTe, g_user_date_format);
var coursedate = today - startDateNum;
//alert(coursedate);
var dayDifference = coursedate / (1000 * 60 * 60 * 24);
alert(dayDifference);
if (dayDifference > 365) {
g_form.addErrorMessage("Course Completed Date should not be more than 365 days");
return false;
}
}
}
}

 

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023