- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-11-2022 03:12 AM
I currently have an onchange client script that will add 3 days to a date field in my service portal. However, I want to make sure I exclude weekends. I currently have a schedule set in cmn_schedule_list.do so how do I point back to this table to look at the specific schedule that excludes weekends?
The scripts I'm currently using below.
onchange client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (newValue == 'Letterhead') {
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addDateAmount');
ajax.getXML(calThreeDays);
}
function calThreeDays(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_delivery_date', answer);
}
}
script include:
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
addDateAmount: function(){
var date = new GlideDateTime(gs.now());
date.addDays(3);
return date.getDate();
},
type : 'ClientDateTimeUtils'
});
Solved! Go to Solution.
- Labels:
-
Service Portal Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 12:53 AM
CATALOG CLIENT SCRIPT:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (newValue == 'Letterhead') {
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addDateAmount');
ajax.getXML(calThreeDays);
}
function calThreeDays(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_delivery_date', answer);
}
}
SCRIPT INCLUDE:
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
addDateAmount: function(){
var date = new GlideDateTime(gs.now());
if(date.getDayOfWeekUTC() == 2){
date.addDays(2); // if MONDAY then add 6 days to get Monday
}
else if(date.getDayOfWeekUTC() == 3){
date.addDays(2); // if TUESDAY then add 7 days to get Monday
}
else if(date.getDayOfWeekUTC() == 4){
date.addDays(4); // if WEDNESDAY then add 8 days to get Monday
}
else if(date.getDayOfWeekUTC() == 5){
date.addDays(3); // if THURSDAY then add 3 days to get Monday
}
else if(date.getDayOfWeekUTC() == 6){
date.addDays(2); // if FRIDAY then add 3 days to get Monday
}
else if(date.getDayOfWeekUTC() == 7){
date.addDays(1); // if SATURDAY then add 6 days to get Monday
}
else if(date.getDayOfWeekUTC() == 1){
date.addDays(0); // if SUNDAY then add 6 days to get Monday
}
return date.getDate();
},
type : 'ClientDateTimeUtils'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-11-2022 05:28 AM
Hi Kevin,
do I understand well, that with adding 3 days you don't want to hit the weekend?
Meaning if you set Tuesday, the result is Friday but if you set Wednesday it will not be Saturday but Monday?
In this case you can utilize the 'getDayOfWeekLocalTime' method and add more days.
var date = new GlideDateTime(gs.now());
if(date.getDayOfWeekLocalTime() == 3) {
date.addDays(5);
}
else if (date.getDayOfWeekLocalTime() == 4) {
date.addDays(4);
}
else {
date.addDays(3);
}
return date.getDate();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-11-2022 02:40 PM
I added the script as follows in my script include, but I still get the same result where it doesn't exclude the weekend. Am I missing something?
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
addDateAmount: function(){
var date = new GlideDateTime(gs.now());
if(date.getDayOfWeekLocalTime() == 3) {
date.addDays(5);
}
else if (date.getDayOfWeekLocalTime() == 4) {
date.addDays(4);
}
else {
date.addDays(3);
}
return date.getDate();
},
type : 'ClientDateTimeUtils'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-12-2022 01:34 AM
thanks for the support.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-11-2022 05:29 PM
Hi Shaan,
Create a 24X7 floating Weekday Schedule and then add below function to your script include.
In the function pass the sys_id of the cmn_schedule 24X7 schedule record to GlideSchedule API.
In the client script change the function name, and also pass another parameter "sysparm_days2add", and give desired number of days you want to add.
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
addDateAmount: function () {
var date = new GlideDateTime(gs.now());
date.addDays(3);
return date.getDate();
},
addDays2Date: function () {
var startDate = new GlideDateTime();
var days = g_form.getParameter("sysparm_days2add");
var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
var schedule = new GlideSchedule("62fd68ff870201108bc2da873cbb35f8"); // Create a 24X7 Weekday schedule in cmn_schedule table and pass the sys_id of the cmn_schedule here
var endDate = schedule.add(startDate, dur);
return endDate.getDisplayValue();
},
type: 'ClientDateTimeUtils'
});
System will calculate future date excluding the weekends.
Please impersonate users who have different time zones set in their profile and test.
You may want to improve it as per your need.
Please check the product documentation link for GlideSchedule API here.
https://developer.servicenow.com/dev.do#!/reference/api/rome/server/no-namespace/c_GlideScheduleScopedAPI#r_ScopedGlideScheduleAdd_GlideDateTime_GlideDuration?navFilter=glideschedule
Thanks and regards,
Subrahmanyam Satti