Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Date Variable on catalog items

KaralapatiN
Tera Contributor

I have a requirement to configure a date variable on a catalog item so that users can only select dates that are at least 10 business days out.

6 REPLIES 6

Dr Atul G- LNG
Tera Patron

Hi @KaralapatiN 

 

Try like

 

(function executeRule(current, previous /*null when async*/) {

    var minStartDate = calculateMinStartDate();

    if (current.start_date < minStartDate) {
        current.setAbortAction(true);
        gs.addErrorMessage("Start date must be at least 10 working days from today. The earliest possible date is " + minStartDate);
    }

    function calculateMinStartDate() {
        var today = new GlideDateTime();
        var workingDaysToAdd = 10;
        var totalDaysToAdd = 0;

        while (workingDaysToAdd > 0) {
            totalDaysToAdd++;
            var tempDate = new GlideDateTime(today);
            tempDate.addDays(totalDaysToAdd);
            var dayOfWeek = tempDate.getDayOfWeekUTC();
            if (dayOfWeek !== 0 && dayOfWeek !== 6) { // 0 = Sunday, 
*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

@Dr Atul G- LNG 

the requirement is for variable on catalog item but the code you shared is different and looks like a business rule which won't run for catalog item.

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

I shared it as a reference to use the logic, not to reuse the same BR.

*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

Dr Atul G- LNG
Tera Patron

https://www.servicenow.com/community/developer-forum/start-date-must-be-10-working-days-ahead-in-cat...

 

https://www.servicenow.com/community/itsm-forum/how-can-i-restrict-a-date-variable-on-a-catalog-item...

*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************