+ Start a Discussion
Marcin KułakowskiMarcin Kułakowski 

Trigger to prevent double event booking and the same booking

Hi Guys,

I've got in my Org two custom objects: Event__c and Registration__c. Basically users can register for an event but the thing is that one user should do it only once for the event and reservation can't be made in future and at events data/time.
I was fighting with the trigger to resolve that problem but unfortunately my Apex and SOQL lack of knowllege succesfully stoped me. So I ask if anyone could help me.Hi Guys,

I've got in my Org two custom objects: Event__c and Registration__c. Basically users can register for an event but the thing is that one user should do it only once per event and reservation shouldn’t be made in the past and at the events data/time.
I was fighting with the trigger to resolve that problem but unfortunately my Apex and SOQL lack of knowledge successfully stopped me in this task completion. So I ask if anyone could help me.

Simplified structure of objects is shown below:

Event__c
_____________________
Event_Type__c    Picklist            
Event_address__c    Text Area            
CreatedById    Lookup(User)                    
Event_Date__c    Date/Time            
Name    Text(80)                           
LastModifiedById    Lookup(User)            
OwnerId    Lookup(User,Group)            

Registration__c
_____________________   
Name    Auto Number           
CreatedById    Lookup(User)           
Event__c    Lookup(Event)            
LastModifiedById    Lookup(User)           
OwnerId    Lookup(User,Group)           

In theory I would check if combination of Registration__c.OwnerId with Event__c.Id already exists in all records of Reservation__c object and if Event__c.Date is not Now or in the past (before insert, before update).

 


Simplified structure of objects is shown below:

Event__c
_____________________
Event_Type__c    Picklist            
Event_address__c    Text Area            
CreatedById    Lookup(User)                    
Event_Date__c    Date/Time            
Name    Text(80)                           
LastModifiedById    Lookup(User)            
OwnerId    Lookup(User,Group)            

Registration__c
_____________________   
Name    Auto Number           
CreatedById    Lookup(User)           
Event__c    Lookup(Event)            
LastModifiedById    Lookup(User)           
OwnerId    Lookup(User,Group)           

In theory I would check if cobination of Registration__c.OwnerId with Event__c.Id alredy exists in all records of Reservation__c object and if Event__c.Date is not Now or in the past (before insert, before update).
Best Answer chosen by Marcin Kułakowski
Raj VakatiRaj Vakati
Even if I use yours or mine operator system still allows me to register an event in the past. 


Use validation rule to controller this one .. no need to change the ant trigger logic


Event_Date__c< TODAY()
.. then through an error 

https://success.salesforce.com/answers?id=9063A000000lEd6QAE

All Answers

Raj VakatiRaj Vakati
Use this code ..Make sure you need to bulkify 
 
trigger Regtrigger on Registration__c (before insert , before update) {
    // Set<Id> evenId = new Set<Id>(); 
    for(Registration__c r : trigger.new ){
        //evenId.add(r.Event__c);
        List<Registration__c> ebe = [Select Id from Registration__c where Event__c =:r.Event__c AND OwnerId=:UserInfo.getUserId() AND Event__r.Event_Date__c>:System.now()];
        if(ebe.size()>0){
            r.addError(' Double Booking not allowed'); 
        }
    }
    
    
}

 
 
Marcin KułakowskiMarcin Kułakowski
Hello Raj,

Thank you so mutch for the solution. It works great for double registration but unfortunately it doesn't work for date case. I had thought that I knew where the problem was. I mean that I had thought there was one mistake in yours query:

I wanted to disallow past registration so second part of logical condition should probably had this operator <=   ?
But still it's not the case. Even if I use yours or mine operator system still allows me to register event in the past. 

 
Raj VakatiRaj Vakati
Change the Query like below 
 
List<Registration__c> ebe = [Select Id from Registration__c where Event__c =:r.Event__c AND OwnerId=:UserInfo.getUserId() AND Event__r.Event_Date__c>=:System.now()];

 
Marcin KułakowskiMarcin Kułakowski
Hello Raj,

Thank you for your reply but still no luck. Record has been added succesfully with date condition.
Raj VakatiRaj Vakati
What all the conditions .. Can you give me clear picture i mean with AND , OR 
Raj VakatiRaj Vakati
List<Registration__c> ebe = [Select Id from Registration__c where Event__c =:r.Event__c AND OwnerId=:UserInfo.getUserId() 
OR Event__r.Event_Date__c>=:System.now()];
Raj VakatiRaj Vakati
. Even if I use yours or mine operator system still allows me to register an event in the past. 


Use validation rule to controller this one .. no need to change the ant trigger logic


Event_Date__c< TODAY()
.. then through an error 

https://success.salesforce.com/answers?id=9063A000000lEd6QAE
Marcin KułakowskiMarcin Kułakowski
Hi Raj,

Below is the code for the trigger that I used:
trigger CheckDoubles on Registration__c (before insert , before update) {
    for(Registration__c r : trigger.new ){
        List<Registration__c> ebe = [Select Id from Registration__c where Event__c =:r.Event__c AND OwnerId=:UserInfo.getUserId() AND Event__r.Event_Date__c>=:System.now()];
        if(ebe.size()>0){
            r.addError('Past Registration is bad Double Booking is also not allowed'); 
        }
    }
    
    
}

I thought that might be the problem with System.Now() so in diffrent try I moved it to variable but with no luck. 
Raj VakatiRaj Vakati
Can you give me an use case and what all the conditions need to be checked .. i will rewrite thw query. i mean AND , OR 
Marcin KułakowskiMarcin Kułakowski
Hi Raj,

Thank you a lot for the idea. Validation rule did the job. 

I used your trigger:
 
trigger CheckDoubles on Registration__c (before insert , before update) {
    for(Registration__c r : trigger.new ){
        List<Registration__c> ebe = [Select Id from Registration__c where Event__c =:r.Event__c AND OwnerId=:UserInfo.getUserId()];
        if(ebe.size()>0){
            r.addError('Double Booking is also not allowed'); 
        }
    }
    
    
}

and I created validation rule for Registration__c a Event__r.Event_Date__c <= NOW()
Raj VakatiRaj Vakati
Cool! Mark is as solved by choosing the best answer 
Marcin KułakowskiMarcin Kułakowski
Done. Thanks a lot for help again :)
Raj VakatiRaj Vakati
I am asking you to choose my answer :) !Still, that is fine .. your problem is solved
Marcin KułakowskiMarcin Kułakowski
Just paste it as your answer and I'll mark it as solved.  This conversation was quite long so your solution ended up in couple of answers.
Raj VakatiRaj Vakati
Even if I use yours or mine operator system still allows me to register an event in the past. 


Use validation rule to controller this one .. no need to change the ant trigger logic


Event_Date__c< TODAY()
.. then through an error 

https://success.salesforce.com/answers?id=9063A000000lEd6QAE
This was selected as the best answer