- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 10:31 AM - edited 12-06-2023 10:54 AM
Hi All,
I need to populate the Expiration date to 3 days from state changes to the solution proposed. date should be from Monday to Friday only.
I have tried the below BR, but it didn't work. nDate value is undefined.
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
// Get current date and time
var gdt = new GlideDateTime();
gs.info("Chaitu gdt: " + gdt);
// Calculate date 3 days after
var nDate = gdt.addDaysLocalTime(3);
gs.info("Chaitu 3 Days: " + nDate);
// Check if nDate is a weekday (Monday-Friday)
while (nDate.getDayOfWeek() == 0 || nDate.getDayOfWeek() == 6) {
nDate = nDate.addDaysLocalTime(1);
gs.info("Chaitu nDate: " + nDate);
}
// Set the desired field to the calculated date
current.u_expiration_date = setValue('nDate');
})(current, previous);
thanks in advance
Chaitanya
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 11:45 AM
It worked partially, Below is full working code
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var gdt = new GlideDateTime();
gs.print("Chaitu gdt: " + gdt);
var getDayOfWeek = gdt.getDayOfWeek();
gs.print(getDayOfWeek);
// Calculate date 3 days after
gdt.addDaysLocalTime(3);
gs.print("Chaitu 3 Days: " + gdt);
// If today is Wednesday, Thursday, Friday (day 3,4,5), add extra days to skip the weekend
if ((getDayOfWeek == 3) || (getDayOfWeek == 4) || (getDayOfWeek == 5)) {
gdt.addDaysLocalTime(2); // Skip Saturday and Sunday
}
// If today is Saturday (day 6), add extra days to skip the weekend
if ((getDayOfWeek == 6)) {
gdt.addDaysLocalTime(1); // Skip Sunday
}
current.setValue("u_expiration_date", gdt)
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 12:28 PM
It's good that your issue resolved here but it's complete surprise that you didn't marked our reply as accepted solution/helpful. @Saurabh Gupta also shared an idea to achieve the desired result.
I don't know what's reason but it's not a good practice here in community. You still have option to do that along with mark helpful @Saurabh Gupta reply as gratitude.
Note: more than one reply can be mark as accepted solution.
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 11:01 AM
Hi,
You can use GlideSchedule API.
Sample-
var startDate = new GlideDateTime('2014-01-02');
var days = 2;
var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
var schedule = new GlideSchedule();
var end = schedule.add(startDate, dur);
gs.info(end);
GlideSchedule | ServiceNow Developers
Thanks and Regards,
Saurabh Gupta