User should not be able to select a day in Record Producer that already has three reservations.

Jeff Loock
Tera Contributor

I have built a record producer that has a reservation start date field. One of my requirements is to not allow a user to select a date that already has three reservations. I understand that I need a script include that can be client callable. I am having trouble calling the field in the record producer and comparing that to the reservations already in my work orders table. Here is what I have thus far for the script include. 

 

var ReservationCountAjax = Class.create();
ReservationCountAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
 
reservationDates: function() {
gs.addInfoMessage('In shuttles date function.');
 
var selectDateParm = this.getParameter('sysparm_date');
var selectedDate = new GlideDateTime(selectDateParm);
var formattedDate = (selectedDate.getByFormat('yyyy-MM-dd HH:mm:ss'));
 
 
var workOrderGR = new GlideRecord('table');
workOrderGR.addQuery('state', 2); 
workOrderGR.addQuery('u_reservation_start', '>=', formattedDate);
workOrderGR.query();
 
gs.addInfoMessage('at line ');
 
var inProgressDates = workOrderGR.getRowCount();
gs.addInfoMessage('Number of reservations =:' + inProgressDates);
 
        var inProgressDates = [];
                
                while (workOrderGR.next()) {
                var requestedDate = workOrderGR.getValue('u_shuttle_start_time');
                inProgressDates.push(requestedDate);
                }
 
        return inProgressDates;
    },
 
 
    type: 'ReservationCountAjax'
});

 

2 REPLIES 2

Tushar
Kilo Sage
Kilo Sage

Hi @Jeff Loock 

 

Can you please try below code and let me know -

 

var ReservationCountAjax = Class.create();
ReservationCountAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
  reservationDates: function() {
    var selectDateParm = this.getParameter('sysparm_date');
    var selectedDate = new GlideDateTime(selectDateParm);
    var formattedDate = selectedDate.getByFormat('yyyy-MM-dd HH:mm:ss');

    var workOrderGR = new GlideRecord('table');
    workOrderGR.addQuery('state', 2); // Only get records in the 'In Progress' state
    workOrderGR.addQuery('u_reservation_start', '>=', formattedDate);
    workOrderGR.query();

    var inProgressDates = [];
    while (workOrderGR.next()) {
      inProgressDates.push(workOrderGR.u_reservation_start.getDisplayValue());
    }

    // Check if there are three or more reservations for the given date
    if (inProgressDates.length >= 3) {
      // The date is already reserved
      return false;
    } else {
      // The date is available
      return true;
    }
  },

  type: 'ReservationCountAjax'
});

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

I put a gs.addErrorMessage after line 6 and it seems as though the field value is still not being pulled in from the record producer correctly.