Change Start and End dates must be only weekend.

Shidhi
Tera Contributor

Please help me to check start and end dates on ''onSubmit' client script 

 

Two fields are there: start_date and end_date on a change form.

These two fields should only accept date and time between Friday 7:00 pm ET - Saturday 11:59 pm ET and Saturday 10:00 pm ET to Sunday 12:00 pm ET. If dates are not selected within these mentioned timings it should throw a warning message.

 

Instance is in ET timezone.

format of start and end date : 2024-03-16 22:00:00

 

Any help is sincerely appreciated.

 

Thank you!

 

 

 

1 ACCEPTED SOLUTION

Amitoj Wadhera
Kilo Sage

Hi @Shidhi ,

 

Here's a sample onSubmit client script to validate the date and time ranges.

 

 

function onSubmit() {
    var startDate = g_form.getValue('start_date');
    var endDate = g_form.getValue('end_date');

    var startDateTime = new Date(startDate);
    var endDateTime = new Date(endDate);

    if (isNaN(startDateTime) || isNaN(endDateTime)) {
        g_form.addErrorMessage('Please enter valid start and end dates.');
        return false;
    }

    var startDay = startDateTime.getUTCDay();
    var startHours = startDateTime.getUTCHours();
    var startMinutes = startDateTime.getUTCMinutes();

    var endDay = endDateTime.getUTCDay();
    var endHours = endDateTime.getUTCHours();
    var endMinutes = endDateTime.getUTCMinutes();

    function toET(date) {
        var offset = -5;
        return new Date(date.getTime() + (offset * 60 * 60 * 1000));
    }

    startDateTime = toET(startDateTime);
    endDateTime = toET(endDateTime);

    startDay = startDateTime.getDay();
    startHours = startDateTime.getHours();
    startMinutes = startDateTime.getMinutes();

    endDay = endDateTime.getDay();
    endHours = endDateTime.getHours();
    endMinutes = endDateTime.getMinutes();

    var isValidStart = (startDay === 5 && startHours >= 19) || 
                       (startDay === 6 && (startHours < 23 || (startHours === 23 && startMinutes <= 59))) || 
                       (startDay === 6 && startHours >= 22) || 
                       (startDay === 0 && startHours <= 12);

    var isValidEnd = (endDay === 5 && endHours >= 19) || 
                     (endDay === 6 && (endHours < 23 || (endHours === 23 && endMinutes <= 59))) || 
                     (endDay === 6 && endHours >= 22) || 
                     (endDay === 0 && endHours <= 12);

    if (!isValidStart || !isValidEnd) {
        g_form.addErrorMessage('Start and end dates must be within the allowed time ranges: Friday 7:00 pm ET - Saturday 11:59 pm ET and Saturday 10:00 pm ET - Sunday 12:00 pm ET.');
        return false;
    }

    return true;
}

 

 

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Shidhi 

you can use GlideAjax with onChange client script on both the fields

what did you start with and where are you stuck?

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

Hi @Ankur Bawiskar 

 

I can't think of the logic to use GlideAjax with the onChange client script on both fields.

 

Below is the business rule that is not working.

 

(function executeRule(current, previous /*null when async*/) {
 
    var fridayStart = new GlideDateTime();
    fridayStart.setDisplayValueInternal('Friday 19:00:00');
    var saturdayEnd = new GlideDateTime();
    saturdayEnd.setDisplayValueInternal('Saturday 23:59:59');
    var infraStart = new GlideDateTime();
    infraStart.setDisplayValueInternal('Saturday 22:00:00');
    var infraEnd = new GlideDateTime();
    infraEnd.setDisplayValueInternal('Sunday 12:00:00');
    var startDate = current.start_date.getGlideObject();
    var endDate = current.end_date.getGlideObject();
    var outsideApprovedWindows = !(startDate>=fridayStart && endDate<= saturdayEnd) && (endDate>=infraStart && endDate<=infraEnd);
    if(outsideApprovedWindows && gs.nil(current.business_justification)){
        gs.addErrorMessage('Business Justification is required for changes scheduled outside the approved change windows.');
        current.setAbortAction(true);
    }
   

 

 

Please help me with this requirement. I am having a little bit of a problem understanding the logic here on identifying if the selected date is Friday, Saturday, Sunday that too needs to take care of the time constraints mentioned. Not sure how to do this.

 

Thank you!

 

@Shidhi 

since you mentioned the instance time is ET then simply do this

1) get the current date/time -> extract day of week i.e. Friday from it -> extract hours from it

2) compare the day of week and hour with user given start date

Similarly do for end date

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

Amitoj Wadhera
Kilo Sage

Hi @Shidhi ,

 

Here's a sample onSubmit client script to validate the date and time ranges.

 

 

function onSubmit() {
    var startDate = g_form.getValue('start_date');
    var endDate = g_form.getValue('end_date');

    var startDateTime = new Date(startDate);
    var endDateTime = new Date(endDate);

    if (isNaN(startDateTime) || isNaN(endDateTime)) {
        g_form.addErrorMessage('Please enter valid start and end dates.');
        return false;
    }

    var startDay = startDateTime.getUTCDay();
    var startHours = startDateTime.getUTCHours();
    var startMinutes = startDateTime.getUTCMinutes();

    var endDay = endDateTime.getUTCDay();
    var endHours = endDateTime.getUTCHours();
    var endMinutes = endDateTime.getUTCMinutes();

    function toET(date) {
        var offset = -5;
        return new Date(date.getTime() + (offset * 60 * 60 * 1000));
    }

    startDateTime = toET(startDateTime);
    endDateTime = toET(endDateTime);

    startDay = startDateTime.getDay();
    startHours = startDateTime.getHours();
    startMinutes = startDateTime.getMinutes();

    endDay = endDateTime.getDay();
    endHours = endDateTime.getHours();
    endMinutes = endDateTime.getMinutes();

    var isValidStart = (startDay === 5 && startHours >= 19) || 
                       (startDay === 6 && (startHours < 23 || (startHours === 23 && startMinutes <= 59))) || 
                       (startDay === 6 && startHours >= 22) || 
                       (startDay === 0 && startHours <= 12);

    var isValidEnd = (endDay === 5 && endHours >= 19) || 
                     (endDay === 6 && (endHours < 23 || (endHours === 23 && endMinutes <= 59))) || 
                     (endDay === 6 && endHours >= 22) || 
                     (endDay === 0 && endHours <= 12);

    if (!isValidStart || !isValidEnd) {
        g_form.addErrorMessage('Start and end dates must be within the allowed time ranges: Friday 7:00 pm ET - Saturday 11:59 pm ET and Saturday 10:00 pm ET - Sunday 12:00 pm ET.');
        return false;
    }

    return true;
}

 

 

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera