Date field should accept only 2 days from today, should restrict past date selection (i.e, yesterday and its past days), and should restrict Future weekends. Help me with script for catalog item.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-20-2020 06:19 AM
Date field should accept only 2 days from today, should restrict past date selection (i.e, yesterday and its past days), and should restrict Future weekends. Help me with script for catalog item.
For example:
1. If Date field selects future weekend it should throw error message "Date field won't accept weekends"
2. if Date field selects more than 2 days from today(i.e,Monday-Friday) should throw error message "Date should not be more than 2 days from today"
3. if Date field select past date(i.e, from yesterday) should throw error message "Date cannot be in past"
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-20-2020 06:34 AM
you need to write function in script include
1) for first problem you need to get day of week
var gdt = new GlideDateTime('2017-10-17 12:00:00');
var dy = gdt.getDayOfWeek();
if(dy=6 ||dy=7){
return false;
}
2) AddDays functionality for glidedateandtime
var date = new GlideDateTime();
date = current.start_date;
date.addDays(25);
3) compare current date with glidedate and time
please go through different function of glidedateandtime

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-20-2020 06:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-20-2020 09:59 AM
Hello Krishna,
You can also try this script for you use case.
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gAjax = new GlideAjax("catalogDate");
gAjax.addParam('sysparm_name', 'functionDate');
gAjax.addParam('sysparm_value', newValue);
gAjax.getXML(returnBack);
function returnBack(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 1) {
//used alert or g_form.addInfoMessage
g_form.addErrorMessage("Date field won't accept weekends");
} else if (answer == 2) {
g_form.addInfoMessage("Date cannot be in past");
} else if (answer == 3) {
g_form.addInfoMessage(" not be more than 2 days from today");
}
}
ScriptInclude:
var catalogDate = Class.create();
catalogDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
functionDate:function()
{
var dateValue = this.getParameter('sysparm_value');
var grDate = new GlideDateTime(dateValue);
var dayValue = grDate.getDayOfWeekLocalTime();
if(dayValue==6 || dayValue==7) // If Date field selects future weekend..
{
return "1";
}
else if(dateValue<gs.daysAgo(1)) //if Date field select past date..
{
return "2";
}
else if(dateValue>gs.daysAgo(-2)) //if Date field selects more than 2 days from today(i.e,Monday-Friday)..
{
return "3";
}
},
type: 'catalogDate'
});
Thanks and regards,
Vanashree Kubal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2020 10:27 PM
Hello Krishna,
If my answer helps you to solve your issue then mark it as correct. So others can also used this code for their reference.
Thanks,