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.

DateDiff in filter condition

HasarinFareeth
Tera Contributor

Hi - I have a Created and Informed Date in my incident form. I wanted to extract the list of incident that has the below condition. How can i achieve this?

Created - Informed Date < 1 Business Day (excluding weekend and bank holidays)

1 REPLY 1

Rushi Savarkar
Kilo Sage
Kilo Sage

Hello @HasarinFareeth 
Try below code

After insert/update BR.

(function executeRule(current, previous) {

    // Get GlideDateTime objects for Created and Informed Date
    var createdDateGDT = new GlideDateTime(current.sys_created_on); // 'sys_created_on' is the standard Created field
    var informedDateGDT = new GlideDateTime(current.u_informed_date); // Replace 'u_informed_date' with your actual Informed Date field name

    // Instantiate the Script Include
    var dateHelper = new IncidentDateHelper();

    // Get the business duration
    var businessDur = dateHelper.getBusinessDuration(createdDateGDT, informedDateGDT);

    // Update the custom duration field
    current.u_business_duration_created_informed = businessDur; // Replace with your custom duration field name

    // Save the current record to persist the updated duration field
    current.update();

})(current, previous);

Script Include:

var IncidentDateHelper = Class.create();
IncidentDateHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getBusinessDuration: function(createdDateGDT, informedDateGDT) {
        if (!createdDateGDT || !informedDateGDT) {
            gs.info("IncidentDateHelper: One of the dates is null.");
            return new GlideDuration(0);
        }
        var schedule = new GlideSchedule(); // Uses the default schedule if no sys_id is provided

        // Calculate the business duration
        var businessDuration = schedule.duration(createdDateGDT, informedDateGDT);
        return businessDuration; // Returns a GlideDuration object
    },

    type: 'IncidentDateHelper'
});

 Let me know if it works

If my response helped you, please accept the solution and mark it as helpful.
Thank You!