How to add current date and 4 business days in catalog item?

keerthilatha
Tera Expert

Hi Team,

We have a requirement as follows:

We need to populate a date in date field in catalog item by using the following logic:

i.e. current date + 4 business days

Please let me know how can we accomplish this requirement.

Thanks & Regards,

Keerthi

1 ACCEPTED SOLUTION

Put this is your default value field:



(function getDateWeekDaysInFuture(addDays) {


        var sdt = new GlideDateTime();



        while (addDays--) {


                  if (isWeekend()) {


                            gs.print('weekend');


                            addDays++;


                  } else {


                            gs.print(sdt.getDate());


                  }


                  sdt.addDaysLocalTime(1);


        }



        return sdt.getDate();



        function isWeekend() {


                  return(sdt.getDayOfWeekLocalTime() == '6' || sdt.getDayOfWeekLocalTime() == '7' );


        }


})(4);



This covers all 3 cases:


  1. Starting date is on a weekend
  2. +4 results on a weekend
  3. +4 is during a weekend

Does not cover public holidays!



On 24/08:



15:22:35.141: 2017-08-24
15:22:35.142: 2017-08-25
15:22:35.142: weekend
15:22:35.142: weekend
15:22:35.142: 2017-08-28
15:22:35.142: 2017-08-29

Remove print statements for production!



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

13 REPLIES 13

Karthik Reddy T
Kilo Sage

Hello Keerthi,



Refer the similar thread may helpful to you.



Need to add 5 business days to a date field.


Karthik Reddy T.
ServiceNow Commnunity MVP -2018 class.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Keerthilatha,



If that should be auto-populated then have it at variable creation itself i.e. default value and make it readonly on form load.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Keerthilatha,



Please populate the default value with following script expression and it should work fine for that catalog variable: for my example it is a date time field


javascript:var futureDate = ''; var gdt = new GlideDateTime(); gdt.addDaysUTC(4); futureDate = new GlideDateTime(gdt.getDate()); futureDate;



It adds 4 days to current date/time and populates in the date/time variable



If it is only date field then following script will work:


javascript:var futureDate = ''; var gdt = new GlideDateTime(); gdt.addDaysUTC(4); futureDate = gdt.getDate(); futureDate;



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur


Thanks. It is date field and I have updated the script in the default value which is provided by you but it is not showing any thing and the field is empty. please find the below screen shot.


find_real_file.png