start date end date

dmahendran
Tera Contributor

I would like to implement a restriction where the end date cannot be selected beyond one month from the start date

18 REPLIES 18

Hello @dmahendran, If you wish to validate based on user's role then you can write a simple script in UI policy like client script as depicted below.

Nishant8_0-1750678897519.png

 

Regards,

Nishant

@Nishant8  I’ve tried several attempts, but it didn’t work as expected. Can we handle this using a Script Include with the clientCallable option enabled? I need to calculate the date difference as part of this logic.

Hello @dmahendran, Yes, it can be validated using CS + SI as well. However, if it's just about calculating the number of days, then the script shared by Ankur should work for you. Otherwise, with SI, a trip from the client to the server will be made. Nevertheless, I'm sharing both the CS and SI below for your reference.

- Script Include script: ensure its client callable

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

    getDaysDifference: function() {
        var startDate = this.getParameter('sysparm_startDate');
        var endDate = this.getParameter('sysparm_endDate');
        if (startDate && endDate) {
            startDate = new GlideDate(startDate);
            endDate = new GlideDate(endDate);
            var diffInMilliseconds = endDate.getNumericValue() - startDate.getNumericValue();
			var diffInDays = diffInMilliseconds / (1000 * 60 * 60 * 24);
			return diffInDays.toString();
        }

    },
    type: 'CompareDates'
});

- Catalog client script: a few points to be considered

  • Script is written on Submit, but can be changed to 'on change' of end date field too
  • if changing to on change, then try to replace getXMLWait() with getXMLAnswer(). Script Include remains same.
function onSubmit() {
	
    var startDate = g_form.getValue('start_date');
    var endDate = g_form.getValue('end_date');
    if (startDate && endDate) {
        var ga = new GlideAjax('CompareDates');
        ga.addParam('sysparm_name', 'getDaysDifference');
        ga.addParam('sysparm_startDate', startDate);
        ga.addParam('sysparm_endDate', endDate);
        ga.getXMLWait(); // doesn't work in portal
        var daysDiff = ga.getAnswer();
        if (daysDiff > 30) {
            g_form.addErrorMessage('Date range is not allowed');
            return false;
        }
    }
}

Regards,

Nishant

Ankur Bawiskar
Tera Patron
Tera Patron

@dmahendran 

you can write onChange client script on end date, like this

You can also role based restriction if you require. please enhance this

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

    g_form.hideFieldMsg('user_id_variable');

    var start = g_form.getValue('start_date');
    var end = g_form.getValue('end_date');
    var minutes = 43200; // 30 days is 43200 minutes

    var startDt = new Date(getDateFromFormat(start, g_user_date_time_format)).getTime() + minutes * 60000; // add minutes to date and then compare
    var endDt = new Date(getDateFromFormat(end, g_user_date_time_format)).getTime();

    if (endDt > startDt) {
        g_form.clearValue('end_date');
        g_form.showFieldMsg('end_date', 'end date cannot be selected beyond one month from the start date', 'error');
    }
}

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

@dmahendran 

Hope you are doing good.

Did my reply answer your question?

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