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.

krishna87654
Kilo Expert

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"

4 REPLIES 4

manish64
Giga Guru

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

https://developer.servicenow.com/dev.do#!/learn/learning-plans/orlando/new_to_servicenow/app_store_l...

 

Vanashree Kubal
Kilo Guru

Hello Krishna,

You can also try this script for you use case.

find_real_file.png

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

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,