Add time limit on Expected delivery

Jyothi76
Tera Contributor

Requirement:

Add time limit for expected delivery based on user's selection of drop-down[Delivery Type: Normal Delivery - 5 days, Express Delivery - 3 days, Express Delivery - 2 days and Express Delivery - 1 day].
In form we have below 2 variables,
1. Delivery Type[Select Box] - In this we have 4 options[Normal Delivery - 5 days, Express Delivery - 3 days, Express Delivery - 2 days and Express Delivery - 1 day].
2. Express delivery[Date/Time].
So here, when we select "Delivery Type" the "Expected delivery" field will auto-populate the date/time.
For example:
User/Me trying  to submit the request on 19/06/2025 and the user/me select the "Delivery Type: Normal Delivery - 5 days", in "Expected delivery - we need to auto-populate the date/time is 25/06/2025".
Note: The Expected delivery will populate based on "Delivery Type", if it 5days,3days,2days and 1day - means we need to calculate the date - based on user's[means Singapore user's] selection from date of ticket submission, and in this we need to exclude Singapore public holidays, Saturdays and Sundays and if public holiday falls on weekends, then the following "Monday" is off day.

Jyothi76_0-1750304938219.png

Thanks,
Jyothi

 

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

@Jyothi76 

for this you can use onChange client script+ GlideAjax

1) get the logged in user location, that will tell you which schedule to use

2) then add those business days using schedule to get the final date, then return that date and set it in variable

check this link on how to add business days

Add Business Days 

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

Hi @Ankur Bawiskar,

Thank you for quick response, the logged in user location  is "Asia/Singapore". Could you please provide the exact onChange client script + GlideAjax,this will help full for me.

Thanks,
Jyothi


@Jyothi76 

I already shared the approach

1) use GlideAjax, pass the delivery type drop down value (5, 10 days etc)

2) in script include function check logged in user location, get the schedule

3) how to add days, I already shared link above.

Unless you start you won't learn

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

Hi @Ankur Bawiskar,

Script Include:

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

    initialize: function() {
        this.singaporeTimeZone = 'Asia/Singapore';
        //var deliveryType = this.getParameter('sysparm_delivery_type');
        //var useCurrentTime = this.getParameter('sysparm_use_current_time');
        //var submitDateStr = this.getParameter('sysparm_submit_date'); //current date/time of submission
        this.singaporePublicHolidays = [
            '2025-01-01',
            '2025-01-29',
            '2025-01-30',
            '2025-03-31',
            '2025-04-18',
            '2025-05-01',
            '2025-05-12',
            '2025-06-07',
            '2025-08-09',
            '2025-10-20',
            '2025-12-25'
        ];
    },

    calculateDeliveryDateAjax: function() {
        var deliveryType = this.getParameter('sysparm_delivery_type');
        var expressType = this.getParameter('sysparm_express_delivery');

        try {
        var deliveryDate = this.calculateDeliveryDate(deliveryType, expressType);
        //return deliveryDate.getDisplayValue();

        if(deliveryDate) {
            var displayValue = deliveryDate.getDisplayValue();
            this.setAnswer(deliveryDate.getDisplayValue());
        }else {
            this.setAnswer('Error: Could not calculate delivery date. ');
        }
    } catch(e) {
        var errorMesg = 'Error in calculateDeliveryDateAjax: ' + e.message;
        this.setAnswer('Error: ' + errorMesg);
    }
},

    calculateDeliveryDate: function(deliveryType, expressType) {
        var startDate = this.getCurrentSingaporeDateTime();
        var daysToAdd = this.getDaysToAdd(deliveryType, expressType);

        if(daysToAdd === 0) {
            return startDate;
        }

        var deliveryDate = new GlideDateTime(startDate);
        var addedDays = 0;

        while(addedDays < daysToAdd) {
            deliveryDate.addDaysUTC(1);

            if(this.isWorkingDaySingapore(deliveryDate)) {
                addedDays++;
            }
        }

        return deliveryDate;
    },

    getCurrentSingaporeDateTime: function() {
        var now = new GlideDateTime();
        now.setTimeZone(this.singaporeTimeZone);
        return now;
    },

    getDaysToAdd: function(deliveryType, expressType) {
        if(deliveryType === 'Normal Delivery') {
            return 5;
        }else if(deliveryType === 'Express Delivery') {
        switch(expressType) {
                case 'Express Delivery – 3 Days':
                return 3;
                case 'Express Delivery – 2 Days':
                return 2;
                case 'Express Delivery – 1 Day':
                return 1;
                default: return 0;

    }
    }
   return 0;

},

isWorkingDaySingapore: function(date) {
    var sgDate = new GlideDateTime(date);
    sgDate.setTimeZone(this.singaporeTimeZone);

    var dayOfWeek = sgDate.getDayOfWeek();
    var dateString = sgDate.getDate().toString();

    if(dayOfWeek === 6 || dayOfWeek === 7) {
        return false;
    }


//var dateString = sgDate.getDate().tostring();
if(this.singaporePublicHolidays.indexOf(dateString) !== -1) {
    return false;

}


    var tempSunday = new GlideDateTime(sgDate);
    //sunday.addDaysUTC(-1);
    tempSunday.addDaysUTC(-(dayOfWeek === 1 ? 2 : (dayOfWeek - 7)));

    var tempSaturday = new GlideDateTime(sgDate);
    //saturday.addDaysUTC(-2);
    tempSaturday.addDaysUTC(-(dayOfWeek === 1 ? 3 : (dayOfWeek - 6)));

    if(dayOfWeek === 1) {
        var prevSunday = new GlideDateTime(sgDate);
        prevSunday.addDaysUTC(-1);
        var prevSundayString = prevSunday.getDate().toString();

        var prevSaturday = new GlideDateTime(sgDate);
        prevSaturday.addDaysUTC(-2);
        var prevSaturdayString = prevSaturday.getDate().toString();

        if(this.singaporePublicHolidays.indexOf(prevSundayString) !== -1 || this.singaporePublicHolidays.indexOf(prevSaturdayString) !== -1) {
            return false;
        }
    }
    return true;
},



    type: 'DeliveryDateCalculator'

});

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

//    if (newValue == 'express') {
//     g_form.setValue('v_campus_printshop_delivery_type', true);
// }

    var deliveyType = g_form.getValue('v_campus_printshop_delivery_type');
    var expressType = g_form.getValue('v_campus_printshop_expected_delivery');

//  if (!deliveyType) {
//     g_form.clearValue('v_campus_printshop_expected_delivery');
//     return;
// }
   
    var expressTypeParam = '';
    if(deliveyType === 'Express Delivery') {
    expressTypeParam = expressType;
    }

    var ga = new GlideAjax('sn_wsd_case.DeliveryDateCalculator');
    ga.addParam('sysparm_name', 'calculateDeliveryDateAjax');
    ga.addParam('sysparm_delivery_type', deliveyType);
    ga.addParam('sysparm_express_type', expressTypeParam);

    ga.getXML(calculateDeliveryDateSG);

    function calculateDeliveryDateSG(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');

        if(answer) {
            g_form.setValue('v_campus_printshop_expected_delivery', answer);
            alert('Expected Delivery');
        }else {
            g_form.setValue('v_campus_printshop_expected_delivery', '');
            alert('Unable to calculate expected delivery. Please try again.' + (answer || ''));
    }
   
    }
}

This is not working, and it is going else part and showing the below alert,
alert('Unable to calculate expected delivery. Please try again.' + (answer || ''));


Thanks,
Jyothi