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

AshishKM
Kilo Patron
Kilo Patron

Hi @Madala Chaitany ,

 

Please try with below updated code and check if nDate value is coming or not

 

 

(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)
    if(nDate.getDayOfWeek() == 0 || nDate.getDayOfWeek() == 6) {
        nDate = nDate.addDaysLocalTime(1);
        gs.info("Chaitu nDate: " + nDate);
    }
    // Set the desired field to the calculated date
    current.setValue("u_expiration_date",nDate); 
   
})(current, previous);

 

Try and share the result.

 

-Thanks,

AshishKMishra

 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Hi @AshishKM ,

 

It's still the same. nDate is undefined ("Chaitu 3 Days: undefined"). nDate value is not coming.

Got it, it's not working because

addDaysLocalTime()

Adds a specified number of days to the current GlideDateTime object. A negative parameter subtracts days. 

The method determines the local date and time equivalent to the value stored by the GlideDateTime object, then adds or subtracts days using the local date and time values. 

 

 

(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); 
    gdt.addDaysLocalTime(3);
    gs.info("Chaitu 3 Days: " + gdt);

    // Check if nDate is a weekday (Monday-Friday)
    if(gdt.getDayOfWeek() == 0 || gdt.getDayOfWeek() == 6) {
        gdt.addDaysLocalTime(1);
        gs.info("Chaitu nDate: " + gdt);
    }
    // Set the desired field to the calculated date
    current.setValue("u_expiration_date",gdt); 
   
})(current, previous);

 

Try this , the last gs.info will print the date.


Please mark this response as correct and helpful if it helps you can mark more that one reply as 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);