- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2016 05:53 AM
Hi,
I am trying to create a client script that will run against a Date variable that will only let end users select days at least 3 business days in the future. I was able to create one that will at least 3 days in future but they are still able to select days during the weekend.
Currently, I am able to achieve this by grabbing the day they selected and comparing it to a future date (today's date + # of days) and clearing the field if it is not enough days out.
Any Ideas?
Thanks,
Josh Anderson
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2016 06:04 AM
The way I do it, without getting into schedules, is to get the day of week value (for example, 4 is Thursday). Then, if day of week value + 3 > 6 (which is Saturday), I add 5 instead of three.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-17-2017 12:32 PM
I have the same requirement could you provide the code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2017 05:47 AM
HI Dilini,
I used a On change - client script along with a Script include to do this... I created this script when I was first learning Javascript and ServiceNow so it can probably be improved upon.
On change Client script
function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading) {
if(newValue != '') {
//Clears previously existing error messages so the error doesn't multiply if user keeps attempting to submit form. I specified which variable to hide below so that the field message (distribution list and functional mailbox disclaimer) does not hide as well, only the error for date hides.
g_form.hideFieldMsg('requested_by_date',true);
//get start date
var selectedDate = g_form.getValue('requested_by_date'); // get the date variable from the form
var compare = '<'; // determine how you want to compare the selected date on the form with the future date
var days = 3; // number of days out for future date from today
var business = 'true'; // Business days only? true or false only
var time = 'false'; // is this a date/time field? true or false only
//glideAJAX that calls on script include and returns back true or false based on the criteria above
var dateAjax = new GlideAjax('scDateUtils');
dateAjax.addParam('sysparm_name','dateCompare');
dateAjax.addParam('day',days);
dateAjax.addParam('selectedDate',selectedDate);
dateAjax.addParam('compare',compare);
dateAjax.addParam('business',business);
dateAjax.addParam('time',time);
dateAjax.getXMLWait();
var validDate = dateAjax.getAnswer();
//alert(validDate);
//If requested by date is in the future or not in a valid date format
if (validDate == 'false') {
g_form.showFieldMsg('requested_by_date','Please choose at least 3 business days in the future.','error'); //Show error
g_form.setValue('requested_by_date',''); //Clear out the Requested by date field
}
}
}
}
Script include
var scDateUtils = Class.create();
scDateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
dateCompare: function() {
var check = true; //check variable is returned at the end and returns true or false base on criteria entered.
var alphaCheck = false; // variable used to determine if any alpha characters are entered.
var weekendCheck = false; //variable used to determine if the date selected is a weekend
var days = this.getParameter('day'); //gets the day parameter from the client
var daysInt = parseInt(this.getParameter('day')); //parses the variable to int so we add/subtract with it
var compare = this.getParameter('compare'); //gets the compare parameter from the client
var business = this.getParameter('business'); //gets the business parameter from the client
var time = this.getParameter('time'); //gets the time parameter from the client
var selectedDateTime = new GlideDateTime(this.getParameter('selectedDate')); //new date variable with date parameter
selectedDateTime.setDisplayValue(this.getParameter('selectedDate'));
var validDate = selectedDateTime.isValid(); //checks to see if the date is a valid date
//gs.log('valid date: '+validDate,'dateutils');
//gs.log('time: ' + time,'dateutils');
//gs.log('business: ' + business,'dateutils');
if (validDate == false){
return false;
}else{
var startDayOfWeek = selectedDateTime.getDayOfWeekUTC();
var endDateTime = new GlideDateTime(); //new date/time variable to repesent the end date
var endDate = new GlideDateTime(endDateTime.getLocalDate()); //extracts only the date from the date/time at the local time
var endTime = endDateTime.getTime(); // end time of the
var endDayOfWeek = endDate.getDayOfWeekUTC(); //get day of the week for the end date
endDate.addDaysLocalTime(days); //adds the days needed to the end-date
if (time == 'true'){
endDate.add(endTime);
var gtime1 = new GlideTime();
gtime1.setValue("00:01:00");
if(compare == '<' || compare == '<='){
endDate.subtract(gtime1);
//gs.log('endate inside time for < <=: ' + endDate, 'dateutils');
}
else if (compare == '>' || compare == '>='){
endDate.add(gtime1);
//gs.log('endate inside time for > >=: ' + endDate, 'dateutils');
}
}
if (time == 'false'){
//if statement determine if there is any alpha characters and if so sets alpha check to true
//gs.log('time/alpha check','dateutils');
if (this.getParameter('selectedDate').match(/[a-z]/i)) {
//gs.log('time','dateutils');
alphaCheck = true;
}
}
//if this date is business days only, then we add two business days to the required date
if (business == 'true'){
if ((endDayOfWeek + daysInt) > 6){
endDate.addDaysLocalTime(2);
}
}
//if it is business days only and the day of the week choosen is saturday or sunday then change weekend check to true
if (business == 'true' && (startDayOfWeek == 6 || startDayOfWeek == 7)){
weekendCheck = true;
}
//gs.log ('weekend check: ' + weekendCheck,'dateutils');
//if it is a valid date and no alpha characters and weekendCheck is false then compare the two dates and return true or false
if (validDate == true && alphaCheck == false && weekendCheck == false ){
if(compare == '>'){
if(selectedDateTime > endDate){
check = false;
}
}
if(compare == '<'){
if(selectedDateTime < endDate){
check = false;
}
}
if(compare == '<='){
if(selectedDateTime <= endDate){
check = false;
}
}
if(compare == '>='){
if(selectedDateTime >= endDate){
check = false;
}
}
if(compare == '=='){
if(selectedDateTime == endDate){
check = false;
}
}
if(compare == '!='){
if(selectedDateTime != endDate){
check = false;
}
}
}else{
check = false;
}
//gs.log( ' selected date: '+ selectedDateTime + ' End Date: ' + endDate + ' compare: ' + compare + ' check: ' +check + ' is Valid: '+ validDate + ' alpha Check: '+alphaCheck + ' weekend check: '+weekendCheck,'dateutils');
return check;
}
},
Thanks,
Josh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2017 08:52 AM
I used following client script and script include. It works
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var distributionDate = g_form.getValue('distribution_date');
var cdt = new Date();
var addtime = 0;
var day = cdt.getDay(); // return 0 for sunday, 1 for monday and store it in selected_day variable.
// alert(day);
if (day == 0 || day == 1 || day == 2) {
addtime = 3;
} else if (day == 3 || day == 4 || day == 5) {
addtime = 5;
} else {
addtime = 4;
}
//var addtime = 3; //The amount of time to add
var addtype = 'day'; //The time type. Can be day, week, month, year.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addDateAmount');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_addtime', addtime);
ajax.addParam('sysparm_addtype', addtype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);
var threeDaysafterCurrentDay = new Date(answer);
//alert(threeDaysafterCurrentDay);
var requestedDistDate = new Date(g_form.getValue('distribution_date'));
//alert(requestedDistDate);
g_form.hideFieldMsg('distribution_date');
if (requestedDistDate <= threeDaysafterCurrentDay) {
g_form.showFieldMsg('distribution_date', "please xxxxxxx" , 'error');
g_form.setValue('distribution_date', '');
}
}
}
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
addDateAmount: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date Field
var addTYPE = this.getParameter('sysparm_addtype'); //What to add - day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = GlideDate();
day.setValue(firstDT);
if(addTYPE == 'day'){day.addDays(addTIME);}
else if (addTYPE == 'week'){day.addWeeks(addTIME);}
else if (addTYPE == 'month'){day.addMonths(addTIME);}
else if (addTYPE == 'year'){day.addYears(addTIME);}
else {day.addDays(addTIME);}
//return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + day;
return day;
},
type: ' ClientDateTimeUtils'
});
Thanks!
Dilini