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.

Check if specific date has active task

tsoct
Tera Guru

Hello, everyone. I have a catalog item that allows users to book space. And the space is reserved after a 'catalog task' created by the flow with updated u_start and u_end (datetime custom fields).

For each start date/time selected by the requestor, the catalog client script should check to see if it is available. And I need assistance with the script below.

Catalog client script:

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

    var ga = new GlideAjax('CheckActiveBooking');
    ga.addParam('sysparm_name', "checkActiveBooking");
    ga.addParam('sysparm_start', newValue);
    ga.getXMLAnswer(function(answer) {
        if (answer != 'false') {
            var parser = JSON.parse(answer);
            alert('No availability at your preferred start time. Please choose another time.');
            return false;
        }
    });

}

Script include:

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

    checkActiveBooking: function() {
        var newbookingStart = this.getParameter('sysparm_start');
        var answer = '';

        var gr = new GlideRecord('sc_task');
        gr.addActiveQuery();
        gr.query();

        if (gr.next()) {
            var startDateTime = new GlideDateTime('u_start');
            var endDateTime = new GlideDateTime('u_end');

            if (newbookingStart.getNumericValue() > startDateTime.getNumericValue() && newbookingStart.getNumericValue() < endDateTime.getNumericValue()) {
                answer = 'true'; //Existing booking found. 
            } else {
                answer = 'false'; //Allow new booking
            }
            return answer;
        }
    },

    type: 'CheckActiveBooking'

});

 

1 REPLY 1

shikhatyagi
Kilo Sage

Hi @tsoct 

 

There is one mistake in the script include.

Please update below 2 lines. 

 

var startDateTime = new GlideDateTime(gr.u_start);

var endDateTime = new GlideDateTime(gr.u_end);

 

 

Please mark my answer as Accepted as Solution & hit Helpful button if it helped you.

 

Thanks,

Shikha