function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
A GunaleA Gunale 

Avoid double booking

I need to avoid booking at the same time i.e one booking is at 7pm to 8pm and another one is booking at 7:30pm to 8pm.
can anyone help to resolve this
Best Answer chosen by A Gunale
Andrew GAndrew G
Hello

Maybe this post will give some help.  It is some help i provided someone previously.  It relates particularly to the Event object, but should give you some direction.

https://developer.salesforce.com/forums/ForumsMain?id=9062I000000IKZPQA4

And you may want to review the testing for date/times - reviewing that one it seems incomplete, but maybe my mind is wandering.
for (Event e : eventList ) { 
    if (e.Id == thisId) { 
        //do nothing 
    } else if( (startDate >= e.StartDateTime && startDate < e.EndDateTime ) ||
               (endDate > e.StartDateTime && endDate <= e.EndDateTime ) ||
               (startDate <= e.StartDateTime && endDate => e.EndDateTime ) ) 
     { 
        Trigger.new[0].adderror('Duplicate booking'); 
    }
 } 

The logic would be if the new Starttime is within the Existing OR new EndTime is within the Existing OR the new Record starts before and ends after the Existing.

HTH

Regards
Andrew

All Answers

Danish HodaDanish Hoda
Hi there,

You can refer https://developer.salesforce.com/forums/?id=906F000000094R2IAI
A GunaleA Gunale
Thank you Danish Hoda for your quick response
I want to prevent booking which is happening at 7:30pm. Infact 7pm to 8pm already booking is there
Andrew GAndrew G
Hello

Maybe this post will give some help.  It is some help i provided someone previously.  It relates particularly to the Event object, but should give you some direction.

https://developer.salesforce.com/forums/ForumsMain?id=9062I000000IKZPQA4

And you may want to review the testing for date/times - reviewing that one it seems incomplete, but maybe my mind is wandering.
for (Event e : eventList ) { 
    if (e.Id == thisId) { 
        //do nothing 
    } else if( (startDate >= e.StartDateTime && startDate < e.EndDateTime ) ||
               (endDate > e.StartDateTime && endDate <= e.EndDateTime ) ||
               (startDate <= e.StartDateTime && endDate => e.EndDateTime ) ) 
     { 
        Trigger.new[0].adderror('Duplicate booking'); 
    }
 } 

The logic would be if the new Starttime is within the Existing OR new EndTime is within the Existing OR the new Record starts before and ends after the Existing.

HTH

Regards
Andrew
This was selected as the best answer
A GunaleA Gunale
Thank you Andrew G