You need to sign in to do that
Don't have an account?
Marcin 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).
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).
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
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.
Thank you for your reply but still no luck. Record has been added succesfully with date condition.
OR Event__r.Event_Date__c>=:System.now()];
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
Below is the code for the trigger that I used:
I thought that might be the problem with System.Now() so in diffrent try I moved it to variable but with no luck.
Thank you a lot for the idea. Validation rule did the job.
I used your trigger:
and I created validation rule for Registration__c a Event__r.Event_Date__c <= NOW()
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