- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2020 06:11 PM
Good Evening All,
I am having hard time adding 3 business days from last working date to due date.
There is a variable called Last working date and using a workflow script i need to add 3 business days to the last working date and append it to the due date field on the RITM.
I have used
var gdt = new GlideDateTime(termDate);
//gdt.addDays(3);
gdt.addDaysLocalTime(3);
//gs.print(gdt.getDate());
gs.print(gdt.getLocalDate());
current.due_date = gdt.getLocalDate();
But it only is adding 3 consecutive days and time is not being updated.
Then i tried
var gdt = new GlideDateTime(termDate);
if(gdt.getDayOfWeekLocalTime() == 3)
gdt.addDaysLocalTime(5);
else if(gdt.getDayOfWeekLocalTime() == 7)
gdt.addDaysLocalTime(3);
else
gdt.addDaysLocalTime(3);
current.due_date = gdt.getDisplayValue();
Which also is not working as it is supposed to.
Can anyone provide me how to achieve it? DateTime functionality is little tricky for me to understand.
So if i select "07-08-2020 17:47:15" the due date should be updated as "07-13-2020 17:47:15"
Thanks in Advance 🙂
pK
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2020 08:32 AM
Thank you Muhammad and Mike for your help:
Here is the code that is working as i wanted to.
var gdt = new GlideDateTime(termDate); //
if(gdt.getDayOfWeekLocalTime() == 3)
gdt.addDaysLocalTime(5);
else if(gdt.getDayOfWeekLocalTime() == 4)
gdt.addDaysLocalTime(5);
else if(gdt.getDayOfWeekLocalTime() == 5)
gdt.addDaysLocalTime(5);
else
gdt.addDaysLocalTime(3);
current.setValue('due_date',gdt);
workflow.scratchpad.dueDate = gdt.getDisplayValue();
Thanks,
pK.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2020 06:50 PM
Yeah I understand that you can simply replce the hardcoded value in the GlideDateTime with your variable and it would be dynamic. The hardcoded value was there for you to quick test in the background script and see result.
Here you go, your code will become dynamic by updating the below line as
var gdt = new GlideDateTime(termDate);
Thanks & Regards,
Sharjeel
Muhammad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2020 07:54 PM
Thank you Muhammad,
Still not working, I guess like Mike said i need to include the schedule to the code, Cause when i used your code it worked fine for wednesday but when i changed the date to thursday it added consecutive days again so i modified the code as below:
var gdt = new GlideDateTime(termDate); //
if(gdt.getDayOfWeekLocalTime() == 3)
gdt.addDaysLocalTime(5);
else if(gdt.getDayOfWeekLocalTime() == 7)
gdt.addDaysLocalTime(3);
else if(gdt.getDayOfWeekLocalTime() == 6)
gdt.addDaysLocalTime(4);
else if(gdt.getDayOfWeekLocalTime() == 4)
gdt.addDaysLocalTime(6);
else
gdt.addDaysLocalTime(3);
current.due_date = gdt;
But then when i used Friday it was calculating only one day. So when ever i use it might be a future date it should calculate 3days from there.
Thanks,
pK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2020 08:03 PM
I finally found the right answer
This below code will add 3 days for all the scenarios.
var gdt = new GlideDateTime(termDate); //
if(gdt.getDayOfWeekLocalTime() == 3)
gdt.addDaysLocalTime(5);
else if(gdt.getDayOfWeekLocalTime() == 4)
gdt.addDaysLocalTime(5);
else if(gdt.getDayOfWeekLocalTime() == 5)
gdt.addDaysLocalTime(5);
else if(gdt.getDayOfWeekLocalTime() == 6)
gdt.addDaysLocalTime(4);
else if(gdt.getDayOfWeekLocalTime() == 7)
gdt.addDaysLocalTime(3);
else
gdt.addDaysLocalTime(3);
Thanks,
pK.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2020 08:37 PM
Hey Muhammad,
Thanks for the code, I got it finally but there is some issue with appending the same due date to the short description field
I got the code corrected but when i append the same to the SD it is adding a day to it. Not sure whats happening
var gdt = new GlideDateTime(termDate); //
if(gdt.getDayOfWeekLocalTime() == 3)
gdt.addDaysLocalTime(5);
else if(gdt.getDayOfWeekLocalTime() == 4)
gdt.addDaysLocalTime(5);
else if(gdt.getDayOfWeekLocalTime() == 5)
gdt.addDaysLocalTime(5);
else
gdt.addDaysLocalTime(3);
current.due_date = gdt;
workflow.scratchpad.dueDate = current.due_date;
if (current.variables.user_not_listed == 'false') {
current.short_description = "Remove access request - " + current.variables.user.getDisplayValue() + ' ' + gdt;
} else {
current.short_description = "Remove access request - " + current.variables.user_username + ' ' + workflow.scratchpad.dueDate;
}
var request = new GlideRecord('sc_request');
if (request.get(current.request)) {
request.requested_for = current.variables.user.toString();
request.update();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2020 04:48 AM
try this for SD.
current.short_description = "Remove access request - " + current.variables.user.getDisplayValue() + ' ' + gdt.toString();
Muhammad