I'm attempting to code a java script to prevent a user from entering a new list entry that has a duplicate name and start date/time to an existing entry.
Essentially it is a vehicle booking system (i'm aware of schdeuler and resourse task types, these have been nuked by IT security for unknown reasons) so I'm forced to try and build one from scratch.
Basically within the new list entry I have a "Vehicle booked" drop down menu and a "Booking Start Time" as a date/time selection. I want sharepoint, when the save button is clicked, to compare those 2 columns to ensure that there isn't already a row that has those values, if there is, prevent the save and show an error message.
Eg, If I book "Kia Carnival" with a start date of 02/05/2023 3:00Pm and another user attempted to book the same vehicle at the same time I want to prevent that.
I've attempted the following variations of the code.
// wait for the SharePoint JavaScript libraries to load
_spBodyOnLoadFunctionNames.push("checkDuplicateEntries");
function checkDuplicateEntries() {
// get the values of the new list item
var newVehicleBooking = $("select[title='Vehicle booking']").val();
var newBookingStartTime = $("input[title='Booking start time']").val();
// loop through all existing list items to check for duplicates
var listName = "Your List Name";
var listURL = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?$select=Vehicle_x0020_booking,Booking_x0020_start_x0020_time";
$.ajax({
url: listURL,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
var items = data.d.results;
for (var i = 0; i < items.length; i++) {
var item = items[i];
// compare the values of the existing list item with the new list item
var existingVehicleBooking = item.Vehicle_x0020_booking;
var existingBookingStartTime = item.Booking_x0020_start_x0020_time;
if (existingVehicleBooking === newVehicleBooking && existingBookingStartTime === newBookingStartTime) {
// show an error message and prevent saving the new list item
alert("Duplicate entry found! Please select a different Vehicle booking or Booking start time.");
$("input[value='Save']").attr("disabled", "disabled");
return false;
}
}
},
error: function (data) {
console.log("Error: " + data);
}
});
}
This one throws the error message as soon as the vehicle is duplicated, regardless of the date selected.
// wait for the SharePoint JavaScript libraries to load
_spBodyOnLoadFunctionNames.push("checkDuplicateEntries");
function checkDuplicateEntries() {
// get the values of the new list item
var newVehicleBooking = $("select[title='Vehicle booking']").val();
var newBookingStartTime = $("input[title='Booking start time']").val();
// loop through all existing list items to check for duplicates
var listName = "Your List Name";
var listURL = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?";
$.ajax({
url: listURL,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
var items = data.d.results;
for (var i = 0; i < items.length; i++) {
var item = items[i];
// compare the values of the existing list item with the new list item
var existingVehicleBooking = item.Vehicle_x0020_booking;
var existingBookingStartTime = item.Booking_x0020_start_x0020_time;
if (existingVehicleBooking === newVehicleBooking && existingBookingStartTime === newBookingStartTime) {
// show an error message and prevent saving the new list item
alert("Duplicate entry found! Please select a different Vehicle booking or Booking start time.");
$("input[value='Save']").attr("disabled", "disabled");
return false;
}
}
},
error: function (data) {
console.log("Error: " + data);
}
});
}
This one doesn't pick up any duplicates
// wait for the SharePoint JavaScript libraries to load
_spBodyOnLoadFunctionNames.push("checkDuplicateEntries");
function checkDuplicateEntries() {
// get the current list item
var currentItemID = GetUrlKeyValue('ID');
var currentItem = null;
if (currentItemID != "") {
currentItem = window.top.frames[0].document.getElementById("idAttachmentsTable").parentNode.nextElementSibling;
}
// get the values of the new list item
var newVehicleBooking = $("select[title='Vehicle booking']").val();
var newBookingStartTime = $("input[title='Booking start time']").val();
// loop through all existing list items to check for duplicates
var listName = "Your List Name";
var listURL = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items";
$.ajax({
url: listURL,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
var items = data.d.results;
for (var i = 0; i < items.length; i++) {
var item = items[i];
// skip the current list item
if (currentItem != null && item.Id == currentItemID) {
continue;
}
// compare the values of the existing list item with the new list item
var existingVehicleBooking = item.Vehicle_x0020_booking;
var existingBookingStartTime = item.Booking_x0020_start_x0020_time;
if (existingVehicleBooking == newVehicleBooking && existingBookingStartTime == newBookingStartTime) {
// show an error message and prevent saving the new list item
alert("Duplicate entry found! Please select a different Vehicle booking or Booking start time.");
window.history.back();
return false;
}
}
},
error: function (data) {
console.log("Error: " + data);
}
});
}
same with this one.
I've also attempted to use the field ID, over the column title, with now joy.
Any ideas?
A cleaner way of doing things would be to filter the items you query instead of requesting the whole list (or implicitely here the first 100 items).
The URL called would be
var listURL = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?$filter=Booking_x0020_start_x0020_time eq '" + dateValue.toISOString() + "' and Vehicle_x0020_booking eq '" + myVehicleName + "'&$select=ID&$top=1";where
dateValueis a Date object andmyVehicleNameis a string (Vehicle_x0020_bookingbegin a standard Text Field) which I assume from your context.