Validation of multiple Date variables present in multiple MRVS

tanuja_g
Tera Contributor

Hi All,

 

We have 4 Date variables in catalog item.

Date Out(variable1) should match either 1st MRVS ABC Details - departure_date(variable2) or 2nd MRVS XYZ Details -  pickup_date(variable3) or 3rd MRVS PQR Details - date_in(variable4) while submitting the form. This condition will avoid user entering incorrect Date out.
If it doesn’t match, pop-up error message, your booking date doesn’t match Date Out. Please verify.

for the above condition one is the normal date variable and remaining 3 date variables are present in 3 different Multi row variable sets in servicenow. And all are Date variables only.

Any help or guidance would be appreciated.

Thanks in advance!

 

5 REPLIES 5

SrilakshmiKH
Tera Contributor

@tanuja_g  Can u specify more about ur requirement.

Ankur Bawiskar
Tera Patron
Tera Patron

@tanuja_g 

you can use onSubmit catalog client script and get value from both MRVS i.e. JSON

Parse that json and then compare the dates.

what script did you start with and where are you stuck?

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

tanuja_g
Tera Contributor

Hi Ankur,

 

I am glad to see your response.

 

I am able to compare the dates successfully. Among the three MRVS (Multi-Row Variable Sets), if none of the dates matches the date_out value, we are able to restrict the user from submitting the form, and the validation works as expected.

However, the issue arises when all three MRVS are empty (no records). In this scenario, while submitting the form, it triggers a JavaScript error. Below is the code I'm using:

 

function onSubmit() {
var date = g_form.getValue('date_out');
var result = false;

// 1st MRVS: 
var mrvs = g_form.getValue('first_mrvs');
mrvs = JSON.parse(mrvs);
var mrvsLength = mrvs.length;

for (var i = 0; i < mrvsLength; i++) {
if (mrvs[i].dpt_date == date) {
result = true;
break;
}
}

// 2nd MRVS: Hotel Details
var mrvs2 = g_form.getValue('second_mrvs');
mrvs2 = JSON.parse(mrvs2);
var mrvsLength2 = mrvs2.length;

for (var j = 0; j < mrvsLength2; j++) {
if (mrvs2[j].date_in == date) {
result = true;
break;
}
}

// 3rd MRVS: Car Hire Details
var mrvs3 = g_form.getValue('third_mrvs');
mrvs3 = JSON.parse(mrvs3);
var mrvsLength3 = mrvs3.length;

for (var k = 0; k < mrvsLength3; k++) {
if (mrvs3[k].pk_date == date) {
result = true;
break;
}
}

if (result) {
g_form.addInfoMessage("Date matched");
} else {
g_form.addErrorMessage("Date not matched");
return false;
}
}

 

Could you please suggest how I can handle the scenario where all MRVS are empty and avoid the JavaScript error?

Thanks in advance for your help!

@tanuja_g 

update as this

 

function onSubmit() {
var date = g_form.getValue('date_out');
var result = false;

// 1st MRVS: 
var mrvs = g_form.getValue('first_mrvs');
var mrvs2 = g_form.getValue('second_mrvs');
var mrvs3 = g_form.getValue('third_mrvs');

if(mrvs && mrvs2 && mrvs3){

mrvs = JSON.parse(mrvs);
var mrvsLength = mrvs.length;

for (var i = 0; i < mrvsLength; i++) {
if (mrvs[i].dpt_date == date) {
result = true;
break;
}
}

// 2nd MRVS: Hotel Details
mrvs2 = JSON.parse(mrvs2);
var mrvsLength2 = mrvs2.length;

for (var j = 0; j < mrvsLength2; j++) {
if (mrvs2[j].date_in == date) {
result = true;
break;
}
}

// 3rd MRVS: Car Hire Details
mrvs3 = JSON.parse(mrvs3);
var mrvsLength3 = mrvs3.length;

for (var k = 0; k < mrvsLength3; k++) {
if (mrvs3[k].pk_date == date) {
result = true;
break;
}
}

if (result) {
g_form.addInfoMessage("Date matched");
} else {
g_form.addErrorMessage("Date not matched");
return false;
}
}
else{
	g_form.addInfoMessage("Please fill data in all 3 MRVS");
	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