Skip to main content
Niamh L asked in #Apex
Hi All, 

I am trying to prevent people double booking a piece of equipment out. The user uses the custom object Booking Request to book out equipment for certain dates. 

For example Mike books equipment1 from the 22/06/2016 until 24/06/2016, then someone else wants to book the same piece of equipment from the 23/06/2016 until 25/06/2016 but should not be allowed as it is already booked.

I have created the following trigger which is working if someone enters the same start date, but how do i stop the record saving if they book in between the start and finish date? Any help greatly appreciated.

trigger DuplicateBookingTrigger on Booking_Request__c (before insert, before update) {

  for (Booking_Request__c br:Trigger.new)

  {

      List<Booking_Request__c> b=[select ID from Booking_Request__c where Equipment_Code__c=:br.Equipment_Code__c and Start_Date__c=: br.Start_Date__c and Finish_Date__c=: br.Finish_Date__c];

      if(b.size()>0)

      {

          br.adderror('The equipment selected is already booked for this date');

      }

   }
17 answers
  1. Jun 1, 2017, 10:46 AM
    Hi Kasper, 

    Unfortunately not. Ended up using a calendar for users to view when items were booked.

    Kind Regards,

    Niamh
0/9000