Restrict the end date field not to select more than 365 days from the start date

Community Alums
Not applicable

Hi All, 

 

Having a requirement that, 

 

need to restrict the end date field not to select the date range more than 365 days from the start date. 

Need assistance on the onchange script to get this sorted. 

 

 

7 REPLIES 7

HrishabhKumar
Kilo Sage

Hi @Community Alums ,

You can use Script include and onLoad client script to create this logic:

I've provide the script below, please customize it according to your requirement.

Script Include:

//Name: DateRangeValidator
//Client Callable: Checked

Script:
var DateRangeValidator = Class.create();
DateRangeValidator.prototype = {
    initialize: function() {},

    validateDateRange: function(startDate, endDate) {
        var startDateObj = new GlideDateTime(startDate);
        var endDateObj = new GlideDateTime(endDate);
        var msInOneDay = 24 * 60 * 60 * 1000;
        var diff = endDateObj.getNumericValue() - startDateObj.getNumericValue();
        var diffDays = diff / msInOneDay;

        if (diffDays > 365) {
            return false;
        }
        return true;
    },

    type: 'DateRangeValidator'
};

 

onLoad Client Script

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

    var startDate = g_form.getValue('start_date');
    var endDate = newValue;

    var ga = new GlideAjax('DateRangeValidator');
    ga.addParam('sysparm_name', 'validateDateRange');
    ga.addParam('startDate', startDate);
    ga.addParam('endDate', endDate);
    ga.getXMLAnswer(function(response) {
        var isValid = response.responseXML.documentElement.getAttribute("answer");
        if (isValid == 'false') {
            g_form.showFieldMsg('end_date', 'End date cannot be more than 365 days from the start date', 'error');
            g_form.clearValue('end_date');
        }
    });
}

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Perhaps this article I wrote 5 years ago helps you in doing this with No code:
- 2019-08-12 - Article - No Code date validations thru (Catalog) UI Policies

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Robbie
Kilo Patron
Kilo Patron

Hi @chanti1,

 

(I'm re-posting my initial and the first response which has a tried and tested solution so this quick, easy and lightweight solution does not get lost in the thread.)

 

This is very easily achieved with little to no code by leveraging a UI Policy as shown in below screen shots.

To create a UI policy, right click on the top of the form you wish to apply it - eg Incident form, and go to 'Configure' > 'Ui Policies'

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

Thanks, Robbie

 

Screenshot 2024-07-25 at 09.45.43.pngScreenshot 2024-07-25 at 09.46.33.png