logo

NJP

Advanced condition in UI action and date comparisions

Import · Mar 11, 2021 · article

We often come across scenarios where we have to put condition in UI Action which cannot be written with a simple condition. e.g. current.active == true or current.canRead() etc, etc.

People often get confused to use advance condition in UI action. Yes, the very common mistake we do is we put javaScript: in UI action condition. Like advanced reference qualifier or filter condions etc, we need not put javaScript: when we are calling a script include from UI action. Moreover, the script include should return true/false as well.

Advanced condtion in a UI action will look like the following snapshot:

image

For example: we are asked to show a UI action when a incident is closed and it should have been closed in last 30 days.

Below is the script include and if you notice the above condition there is no javaScript: there:

getCondition: function(currentObj) {

        // If state is not closed, then directly return false

        if (currentObj.state != 3) {
            return false;
        }

        // otherwise, compare closed date is within last 30 days

        var nowDateTime = new GlideDateTime();

        var dur = new GlideDateTime();

        nowDateTime.setDisplayValue(gs.nowDateTime());

        var closedDateTime = currentObj.closed_at.getDisplayValue();

        dur = gs.dateDiff(closedDateTime, nowDateTime);

        var dateDifference = new GlideDuration(dur);

        var d = dateDifference.getDayPart();

        if (d <= 30) {
            return true;
        }
        return false;
},
View original source

https://www.servicenow.com/community/now-platform-articles/advanced-condition-in-ui-action-and-date-comparisions/ta-p/2318664