emha
Automation Master
Automation Master

Re: How to check if a Date & Time falls within a range and avoid double booking?

Jump to solution
  • How to add a title to the PopUp window to say (WARNING)

that's not possible with OOTB javascript's dialog methods.

you'd need to call SP's dialog to be able to customize dialog, see - SP.UI.ModalDialog Methods | Microsoft Docs 

  • How to make the alert show all the already booked dates instead of poping a window for each one
    (imagine you have 10 booked dates, current scenario the alert will be triggered 10 times continuesly)

move alert out of the loop. within the loop prepare just a message content to be shown afterwards

  • Display the Date/Time in following format dd/MM/yyyy HH:MM in the popup alert

see various OOTB Date object's conversion/output methods - JavaScript Date Reference 

  • How to also trigger the alert when the SUBMIT button is clicked

configure button's 'Client click' setting to make a call to your validation function

  • Any other improvements to optimise the script?

not really improvements/optimizations, just few notes

- do not rely on client side validation like this. whatever effort you spent on single client, you can never avoid in concurrent environment that two or more clients/users do the booking at about the same time and book the same resource for the same time span in parallel. it may help to some extent, but it will never be 100% reliable

- you source in all the bookings at the form load. so all the bookings committed (or possibly released) during the form session will not be considered

0 Kudos
Reply
bimi82
Workflow Hero

Re: How to check if a Date & Time falls within a range and avoid double booking?

Jump to solution

Any chance you can help me in changing the script to meet the two criteria;

- How to make the alert show all the already booked dates instead of poping a window for each one

- Display the Date/Time in following format dd/MM/yyyy HH:MM in the popup alert

The guy who scriptted it is not available and I would not have a slitest idea how to do it

As per your notes:

Yes, I agree, any requests which happen in same time won't be validated.
Therefore, a Workflow validation needs to be in place as well and I will struggle with ths one as well ... gosh!


Though this service is not highly / frequently used and the likelyhood of booking of same date/time is relatively low but not impossible. 

0 Kudos
Reply
bimi82
Workflow Hero

Re: How to check if a Date & Time falls within a range and avoid double booking?

Jump to solution

Final Validation Script:
(which I paid for to be done... but am willing to share with you for free as a payback contribution)

 

var allItems = [];
var listName = 'P...Car Request'
NWF$(document).ready(function() {
retriveListItem();
NWF$('select[name*="DateHours"],select[name*="DateMinutes"],input.nf-date-picker').change(function() {
checkOldEntries();
});

});

function retriveListItem() {
    NWF$.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('" + listName + "')/items?$select=*,PoolCar/CarRegistration&$orderby=FromDate&$expand=PoolCar&$top=5000",
        type: 'GET',
        headers: {
            "Accept": "application/json;odata=verbose"
        },
        cache: false,
        success: function(data) {
            for (var i = 0; i < data.d.results.length; i++) {
                var item = data.d.results[i];
                item.sDate = getDateLocalFormat(item.FromDate,-5);
                item.eDate = getDateLocalFormat(item.UntilDate,5);
                allItems.push(item);
            }
        },
        error: function(data) {
            console.log(error);
        }
    });
};

function getDateLocalFormat(inputDate,min) {
    //var add_10_minutes_to_current_date = Date.dateAdd(new Date(inputDate), -5, "h");
    return Date.dateAdd(new Date(inputDate), min, "m");
    //return new Date(inputDate);
};

Date.dateAdd = function(currentDate, value, timeUnit) {
    timeUnit = timeUnit.toLowerCase();
    var multiplyBy = {
        w:604800000,
        d:86400000,
        h:3600000,
        m:60000,
        s:1000
    };
    var updatedDate = new Date(currentDate.getTime() + multiplyBy[timeUnit] * value);
    return updatedDate;

};

function checkOldEntries() {
    //var count = 0;
     var allBookings='';
    var enteredSDate = new Date(ddmmyyTommddyy(NWF$('#' + validateFromDate).val()) + ' ' + NWF$('select[name*="DateHours"]:eq(0)').val() + ':' + NWF$('select[name*="DateMinutes"]:eq(0)').val());
    var enteredEDate = new Date(ddmmyyTommddyy(NWF$('#' + validateUntilDate).val()) + ' ' + NWF$('select[name*="DateHours"]:eq(1)').val() + ':' + NWF$('select[name*="DateMinutes"]:eq(1)').val());
    if (!(enteredSDate === undefined) && !(enteredEDate === undefined))
        NWF$.each(allItems, function(i, v) {
               if(v.PoolCar.CarRegistration == NWF$('#' + validatePoolCar).val().split('#')[1])
               {
                   
            if (((enteredSDate > v.sDate && enteredEDate < v.eDate) || (enteredEDate > v.sDate && enteredEDate < v.eDate) || (enteredSDate > v.sDate && enteredSDate < v.eDate)) || (enteredSDate <= v.sDate && enteredEDate >= v.eDate) ) {
                       if(allBookings==''){
                            allBookings='The selected pool car '+v.PoolCar.CarRegistration+' is already booked on \n';
                       }
                        allBookings+='\n'+dateFormatddMMyyyyHHmm(v.sDate) +' >> '+dateFormatddMMyyyyHHmm(v.eDate);
                  console.log(v);
               }}
        });
          if(allBookings!=''){
                allBookings+='\n\nPlease choose a different date.';
               alert(allBookings);
               allBookings='';
          }
         
         
         
};
function dateFormatddMMyyyyHHmm(inputDate) {
    var d = new Date(inputDate),
        dformat = [
                    d.getDate().padLeft(),(d.getMonth()+1).padLeft(),
                    d.getFullYear()].join('/')+
                    ' ' +
                  [ d.getHours().padLeft(),
                    d.getMinutes().padLeft()].join(':');
                         //,d.getSeconds().padLeft()].join(':');
                         return dformat;
};
function ddmmyyTommddyy(inputDate) {
    var dateS = inputDate.split('/');
    return dateS[1] + '/' + dateS[0] + '/' + dateS[2];
};


Number.prototype.padLeft = function(base,chr){
   var  len = (String(base || 10).length - String(this).length)+1;
   return len > 0? new Array(len).join(chr || '0')+this : this;
}
   
$('#showit').click(function(){
  
     $('#result').html(dformat);
});
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

Reply
lizmcconel
Nintex Newbie

Re: How to check if a Date & Time falls within a range and avoid double booking?

Jump to solution

Hello! How does this form look like? I need to update a form on my car renting site, on page rent a car in Bulgaria.

0 Kudos
Reply
ghegedus
Canvas Initiate

Re: How to check if a Date & Time falls within a range and avoid double booking?

Jump to solution

Thanks for sharing this.  Quick question: did you find a way to disable submitting the request after the popup was closed please?  

0 Kudos
Reply