Check Calendar for Double Bookings

  • 13 December 2018
  • 11 replies
  • 202 views

Badge +5

There seemed to be many ways to do this, but all adhoc and no complete solutions. So I present the following. happy.png Example in bottom screenshot.

 

In your Nintex Form (classic).

 

1. Define "StartTime" as the Client ID JavaScript variable name for the Start Time control.

 

2. Define "EndTime" as the Client ID JavaScript variable name for the End Time control.

 

3. (Optional) - Remove the Recurrence label and control from the form. If you don't, it will be hidden anyway.

 

4. Copy and paste the JavaScript code from the attachment into your favorite Text Editor like Notepad++. Edit the first line and change "Calendar" to the Title of your SharePoint Calendar that your checking for double bookings.

You can also update the Error Text value on the second line if you wish to change the default.

 

Note: This solution is working on the assumption that your region date format is dd/MM/yyyy. If you need to use MM/dd/yyyy, please un-comment Line 29 (remove leading 2 forward slashes). Then comment Lines 25 & 26 (add 2 forward slashes to the start of each line).

 

5. Copy and Paste the updated JavaScript into Custom JavaScript within Nintex Form Settings.

 

6. Save and Publish your form.

 

If there is an existing booking an error is displayed and the Save buttons are disabled. When a period is specified that doesn't already have a booking, the Save buttons are enabled again and the error is no longer displayed.

Example:

 

I have also expanded the solution to work with an additional column e.g. a resource like a Hot Desk. So if you have multiple Hot Desks or resources. It would validate that, that resource isn't booked already.

 

Enjoy! This one took a while to put together.


11 replies

Badge +3

Nice post thank you, but I guess this isn't function for 24hours date format. Only for 12 hours isn't it?

I tried several time without success. Any help with that? And one more question If I have on Button (Save) my own value it's OK?

Badge +5

Hi heget‌,

It uses the date/time picker control, so not 24hr format. Just the value selected in your control. Also you can have any label applied to your save button.

What issues were you facing?

Badge +3

But for me this solution doesnt work.

In the code if you check  (hoursArray == AM) 12 etc.

But we use 24h region format so maybe this could be the reason ...

Or this is compatible for each "Local date/time" 12 or 24 format. ? ;)

Badge +3

So I've chcked in Mozilla debugger and get this error on opening form.

RangeError: invalid date NewForm.aspx:1504:19
GetISODateString https://URL/Lists/SPZ1/NewForm.aspx:1504:19
CheckDates https://URL/Lists/SPZ1/NewForm.aspx:1517:31
<anonymous>

 

var isoDate = new Date(fullDate.getTime() - (fullDate.getTimezoneOffset() * 60000)).toISOString();

 

Any help ?

Badge +3

So I've tested again on EN regional settings with format of date 01/01/1991 and timezone 12h. It works perfect, but there is problem when I change to 24h. format.

And also if is changed regional setting to another language because then you have date liek this 01. 01. 1991 and the script can't work anymore.

So is there any chance how to edit the script for working also with those regional settings ?

Userlevel 5
Badge +14

Hi @heget,


 


I've updated @shaneoss' script so that it works for dates with a spaces in their formatting.


 



 


 


 


there are several bugs in NINTEX code that cause NINTEX do not correctly works with date formats that include a space, or include (accidentally) one more space, or doesn't include a space but space is expected, or a different separator is used then expected, etc.,


PLENTY OF problem in date/time handling in nintex products, especially if you use other then EN/US locale.


unfortunatelly, NINTEX refuses to fix it!


 


may I ask you what exact locale do you use?


could you perharps log an incident with NINTEX support that you experience problems with these date formats?


if they had more incidents reported for the problem, they might, hopefully, took it a bit more seriously...


 


 

Badge +5

Thanks @emha! :smileyhappy:


 


Sorry @heget missed your previous posts. Yes no consistency with dates and locales across the Nintex suite as emha has mentioned. Wish there was. 🙂 It would make coding custom Nintex solutions alot easier for the global audience where time/date formats were involved.


 


Any issues just lets us know.


Cheers,


Shane

Badge +5

@heget check out @emha's modified version of my script. If your still having issues let me know and what locale your using. Then i'll update my to match mine and do some further testing.Thanks!

Hello, @shaneoss where in your modified code do you add the script to take into consideration the resources field and the start and end dates? What is that field named? What type of field is it? If the resources field allows the user to select resources 1, 2, and 3 and if someone has all 3 resources booked already it will throw an error if someone is trying to also book resource 1, 2, or 3 on the same date(s). Or is each resource a separately named field?

Badge +5

Hi @bdupree,


We have a resource called HotDesk which is a list lookup to another list on the site referencing a single line of text field on the list.


You can see in the code below in the CAML the resource (HotDesk) is added.


 


Easier to copy andpaste into Notepad++


 var startDate = NWF$("#" + StartTime).val();
var endDate = NWF$("#" + EndTime).val();
var hotDesk = NWF$("#" + HotDesk).val();
if (startDate != "" && endDate != "" && hotDesk != "")
{
if (hotDesk.indexOf(";#") != -1)
{
var hotDeskval = hotDesk.split(";#");
hotDesk = hotDeskval[1];
}
var startHours = NWF$('#' + StartTime).parent().find("select[id*='DateHours']").val();
var startMinutes = NWF$('#' + StartTime).parent().find("select[id*='DateMinutes']").val();
var endHours = NWF$('#' + EndTime).parent().find("select[id*='DateHours']").val();
var endMinutes = NWF$('#' + EndTime).parent().find("select[id*='DateMinutes']").val();
var isoStartDateStr = GetISODateString(startDate, startHours, startMinutes);
var isoEndDateStr = GetISODateString(endDate, endHours, endMinutes);
var clientContext = SP.ClientContext.get_current();
var web = clientContext.get_web();
var list = web.get_lists().getByTitle('Region Hot Desks');
var camlQuery = new SP.CamlQuery();
var query = String.format("<View><Query><Where><And><Or><Or><And><Leq><FieldRef Name='EventDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value></Leq><Gt><FieldRef Name='EndDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value></Gt></And><And><Lt><FieldRef Name='EventDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value></Lt><Geq><FieldRef Name='EndDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value></Geq></And></Or><Or><And><Leq><FieldRef Name='EventDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value></Leq><Geq><FieldRef Name='EndDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value></Geq></And><And><Geq><FieldRef Name='EventDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value></Geq><Leq><FieldRef Name='EndDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value></Leq></And></Or></Or><Eq><FieldRef Name='Hot_x0020_Desk' /><Value Type='Text'>{2}</Value></Eq></And></Where></Query></View>", isoStartDateStr, isoEndDateStr, hotDesk);
var currentItemID = GetUrlKeyValue("ID");
if (currentItemID != "")
query = String.format("<View><Query><Where><And><And><Or><Or><And><Leq><FieldRef Name='EventDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value></Leq><Gt><FieldRef Name='EndDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value></Gt></And><And><Lt><FieldRef Name='EventDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value></Lt><Geq><FieldRef Name='EndDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value></Geq></And></Or><Or><And><Leq><FieldRef Name='EventDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value></Leq><Geq><FieldRef Name='EndDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value></Geq></And><And><Geq><FieldRef Name='EventDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value></Geq><Leq><FieldRef Name='EndDate' /><Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value></Leq></And></Or></Or><Neq><FieldRef Name='ID' /><Value Type='Integer'>{2}</Value></Neq></And><Eq><FieldRef Name='Hot_x0020_Desk' /><Value Type='Text'>{3}</Value></Eq></And></Where></Query></View>", isoStartDateStr, isoEndDateStr, currentItemID, hotDesk);
camlQuery.set_viewXml(query);
var listItems = list.getItems(camlQuery);
clientContext.load(listItems);

First CAML (end):


<Eq><FieldRef Name='Hot_x0020_Desk' /><Value Type='Text'>{2}</Value></Eq></And></Where></Query></View>", isoStartDateStr, isoEndDateStr, hotDesk);


 


Second CAML (end):


<Eq><FieldRef Name='Hot_x0020_Desk' /><Value Type='Text'>{3}</Value></Eq></And></Where></Query></View>", isoStartDateStr, isoEndDateStr, currentItemID, hotDesk);


 


Note there's the additional <And> at the beginning accompanying the closing tag.


 


If you needed to add additional resources say up to three you'd need to update the queries to include your additional fields.


 


Hope this helps. :)

Badge +4

Hi Shaneoss,


we tried your JS within our On premise farm, however the form just stuck there keep loading the form will not appear, wonder how to resolve this?

Reply