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

Anand Kumar P
Giga Patron
Giga Patron

Hi @2022_ServiceNow,

Try to update below script in your 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_your_schedule'; // Replace with the actual sys_id
var plannedTaskAPI = new SNC.PlannedTaskAPI();
var response = plannedTaskAPI.calculateDuration(startDate, endDate, scheduleId);
var response = new JSON().decode(resp);

var duration = response.duration;

var weekdays = parseInt(duration.split(' ')[0]);

if (weekdays < 7) {
return true;
} else {
return false;
}
},

type: 'end_date'
});

 

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand

Thanks for your response @Anand Kumar P 

I have changed, still it's not working. Do we need to modify the schedule?

Hi,

Did you debug it by adding logs?

What is not working?

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi @Anil Lande , yes and I got the solution.