Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Validate the dates in the catalog item

2022_ServiceNow
Tera Expert

Hi All,

I have a requirement for the catalog item with two variables, 'start_date' and 'end_date.' The condition is that if the end date is less than 7 business days (Monday to Friday) from the selected start date, the field value should be cleared, and a message should be displayed. I have an onChange client script.

For example, if we select today's date (09/01/2024) as the start date, it should exclude Saturday and Sunday and then calculate 7 days from the start date. Therefore, the end date should be on 18/01/2024.

On Change catalog client script:

 

 

 

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

    var start_date = g_form.getValue('start_date');
    var end_date = g_form.getValue('end_date');

    if (start_date && end_date) {
        var validateEndDate = new GlideAjax("end_date");
        validateEndDate.addParam("sysparm_name", "schedule");
        validateEndDate.addParam("sysparm_start", start_date);
        validateEndDate.addParam("sysparm_end", end_date);
        validateEndDate.getXMLAnswer(function(answer) {
            if (answer == "true") {
                g_form.clearValue('end_date');
                g_form.showFieldMsg('end_date', 'End Date cannot be less than 7 weekdays from the Start Date', 'error');
            }
        });
    }
}

 

 

 

Script Include:

 

 

 

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

    schedule: function() {
        var startDate = this.getParameter("sysparm_start"));
        var endDate = new GlideDateTime(this.getParameter("sysparm_end"));
        var scheduleId = 'sys_id of the schedule';
        var plannedTaskAPI = new SNC.PlannedTaskAPI();
        var resp = plannedTaskAPI.calculateDuration(startDate, endDate, scheduleId);
        var response = new JSON().decode(resp);
        var duration = response.duration;
        var dur = duration.split(' ');
        if (dur[0] < 7) {
            return true;
        } else {
            return false;
        }
    },

    type: 'end_date'
});

 

 

 

This is the schedule:

2022_ServiceNow_0-1704819114997.png

Thanks in advance!

 

@Ankur Bawiskar @Dr Atul G- LNG @Rahul Talreja @Maik Skoddow @Danish Bhairag2 @Pavankumar_1 @Anil Lande @Sainath N @Saurabh Gupta @Tai Vu 

1 ACCEPTED SOLUTION

2022_ServiceNow
Tera Expert
  var startDate = new GlideDateTime(this.getParameter("sysparm_start"));

View solution in original post

6 REPLIES 6

Can you post the solution as well please?

2022_ServiceNow
Tera Expert
  var startDate = new GlideDateTime(this.getParameter("sysparm_start"));