The CreatorCon Call for Content is officially open! Get started here.

Need to auto populate date on the expiration date

Madala Chaitany
Giga Guru

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 

1 ACCEPTED SOLUTION

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);

View solution in original post

6 REPLIES 6

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

Saurabh Gupta
Kilo Patron

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