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

Please check if this helps.



Client Script:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


if (isLoading || newValue === '') {


return;


}


//Type appropriate comment here, and begin script below


var gr = new GlideAjax('MyDateTimeAjax');


gr.addParam('sysparm_name', 'calDate');


gr.addParam('sysparm_start_date', newValue);


gr.getXML(ajaxResponse);


function ajaxResponse(serverResponse) {


alert(answer);


var answer = serverResponse.responseXML.documentElement.getAttribute("answer");


g_form.setValue('Field Name where you want to set the value', answer);


}


}



client callable Script Include with name MyDateTimeAjax.


var MyDateTimeAjax = Class.create();


MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


calDate: function () {


var sdt = new GlideDateTime(this.getParameter('sysparm_start_date'));


sdt.addDaysLocalTime(4);


if(sdt.getDayOfWeekLocalTime() == '6' || sdt.getDayOfWeekLocalTime() == '7') //This will check, the date after adding 4 days is Saturday or Sunday


sdt.addDaysLocalTime(2);


return sdt.getDate();


},


type: 'MyDateTimeAjax'


});



http://wiki.servicenow.com/index.php?title=GlideDateTime#getDayOfWeekLocalTime.28.29


Hi Keerthilatha,



When I executed it in background it is working fine. Can you check once again. it is working for me as well in catalog item variable.



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

Yes, it is coming now but it should only calculate week days, please exclude Saturday and Sunday. So the date should be 2017-08-29.


please check


find_real_file.png


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

I just tried this solution but the default value is today's date. Regardless of what date I pick nothing special happens. I can select any weekend day without an issue. Does this solution still work today or is there something else that I'm missing? I would've thought the default would be today +4 business days excluding weekends.