Prevent Submitting the form if Multi row variable set is empty
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2022 08:44 PM
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2022 09:05 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2022 09:42 PM
@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.
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2022 10:17 AM
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;
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2022 10:34 AM
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;
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2022 06:49 PM - edited 12-08-2022 06:50 PM
@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.
ServiceNow Community Rising Star, Class of 2023