Business days Issue

Dave_p
Giga Guru

Hi,

I am in a scenario where I should i be allowed to select next 15 business days from today. I tried getting some help from community, but it didn't work. Kindly help.

 

1.png

 

2.png

 

3.png

 

4.png

 

5.png

 

6.png

 

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

    var schId = "090eecae0a0a0b260077e1dfa71da828";
    var sch = new GlideSchedule(schId);
    var now = new GlideDateTime();
    var next15Days = sch.add(now, 15, "business");
    var dateThreshold = new GlideDateTime(next15Days).getDate();
    if (current.desired_completion_date < dateThreshold) {
        gs.addErrorMessage("Date must be after 15 business days.");
        current.setAbortAction(true);
    }

})(current, previous);

 

Regards

Suman P.

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Dave_p 

you should use onChange catalog client script rather than BR

check this and enhance

Variable Date no less than 5 business days 

Auto-populating and validating date fields 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

Hi Dave - 43200 is how many seconds are in 12 hours(the schedule of 8-8 is 12 hour business days).  

View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

@Dave_p 

you should use onChange catalog client script rather than BR

check this and enhance

Variable Date no less than 5 business days 

Auto-populating and validating date fields 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

I will try this now Ankur.

Dave_p
Giga Guru

Hi @Ankur Bawiskar,

 

What is 1000 please?

 

Regards

Suman P.

Dave_p
Giga Guru

This is the final code which is updated to work for 15 Business Days for 8-5 excluding weekends.

 

THERE IS NOTHING GREAT ABOUT ME. All respect to @booher04 

 

1.png

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var ga = new GlideAjax('date_Class');
    ga.addParam('sysparm_name', "date_Function");
    ga.addParam('sysparm_date', g_form.getValue('desired_completion_date'));
    ga.getXMLAnswer(function(answer) {
        if (answer == 'true') {
            alert('Please select date after 15 business days');
            g_form.clearValue('desired_completion_date');
        }

    });

}

 

2.png

 

var date_Class = Class.create();
date_Class.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    date_Function: function() {
        var dateSelected = this.getParameter('sysparm_date');
        var nowDateTime = new GlideDateTime();
        var days = 15;
        var dur = new GlideDuration(days * 32400 * 1000);
        var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); //8-5 weekdays excluding holidays
        var finalTime = schedule.add(nowDateTime, dur, '');
        var updatedGdt = new GlideDateTime(dateSelected);
        var finalTimeGdt = new GlideDateTime(finalTime);
        if (updatedGdt < finalTimeGdt) {
            return 'true';
        }
        return 'false';
    },
type: 'date_Class'
});

 

Regards

Suman P.