- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2017 07:53 AM
Hi,
I am new to service-now and facing some issues.
I have two date field variables in my catalog item and i need to do some validations. I tried but not able to achieve this.
My requirement is -
a) Start date should be less than the end date and start date should not be less than current date. If it happens then throw error.
b) End date should not be less than current date and start date, If it happens then throw error.
Please help with examples.
Thanks
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2017 11:52 PM
Ok try this simple script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var startDate = g_form.getValue("start_date");
var endDate = g_form.getValue("end_date");
var format = g_user_date_time_format;
var startDateMs = getDateFromFormat(startDate, format);
var endDateMs = getDateFromFormat(endDate, format);
//alert("Hi "+startDate);
//alert("end" + endDate);
if (startDateMs > endDateMs ) {
g_form.addErrorMessage(new GwtMessage().getMessage("{0} must be after {1}", g_form.getLabelOf("end_date"), g_form.getLabelOf("start_date")));
return false;
}
}
You should run this script on end date check this screenshot:
Thanks
Shruti
If the reply was informational, please like, mark as helpful or mark as correct!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2017 11:52 PM
Ok try this simple script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var startDate = g_form.getValue("start_date");
var endDate = g_form.getValue("end_date");
var format = g_user_date_time_format;
var startDateMs = getDateFromFormat(startDate, format);
var endDateMs = getDateFromFormat(endDate, format);
//alert("Hi "+startDate);
//alert("end" + endDate);
if (startDateMs > endDateMs ) {
g_form.addErrorMessage(new GwtMessage().getMessage("{0} must be after {1}", g_form.getLabelOf("end_date"), g_form.getLabelOf("start_date")));
return false;
}
}
You should run this script on end date check this screenshot:
Thanks
Shruti
If the reply was informational, please like, mark as helpful or mark as correct!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2017 12:49 AM
Hi Shruti,
Original script is working. Sorry i was trying the code on start date variable. But now its working. But the problem is it is not capturing the other requirement.
Start date should not be less than the current date.
I created and another script for start date variable and put this code-
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var startDate = g_form.getValue("newuser_startdate");
var format = g_user_date_format;
if (startDate === "")
return true;
// get date strings into a number of milliseconds since 1970-01-01
var startDateMs = getDateFromFormat(startDate, format);
//alert("Hi "+startDateMs);
// 0 from "getDateFormat" means an invalid date string was passed to it
if (startDate === 0)
g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("newuser_startdate")));
return false;
}
But its not working.Please help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2017 08:48 AM
In that case try this second simple script I pasted it seems to be working, I tested on change request form.
Thanks
Shruti
If the reply was informational, please like, mark as helpful or mark as correct!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2018 09:20 AM
hi shruti,
i tried your code but it was not working,it is accepting the previous dates also...please help me...i'm new to servicenow...i'm trying to do date validations using client script, awaiting for your reply
Thanks
Deepu