-
Trigger to prevent Date Overlap #ApexTrigger
Hi everyone,
I am a newbie Salesforce Developer. And I am new to this forum as well :).
I am trying to write a trigger for a requirement where a new record should not be created if the dates overlap. There are 2 objects Event1 (Campaign renamed) and Attendee. There is a junction object Event Attendee which allows an attendee to register for an Event. If an attendee is already registered for an event then he/she should not be able to register for any other event which falls on those dates or whose start or end dates overlap the already registered event dates. So this trigger that I wrote lets me create records for overlapping dates. If there is one EA record for an Attendee with dates 2/4/2019 and 3/4/19 as start and end dates respectively, there shouldn't be another EA record for an Attendee with dates overlapping 2/4/2019 and 3/4/2019.
Please help me fix my logic. Thanks.
trigger Eventdatesoverlapprevention on Event_Attendee__c (before insert,before update) { set<string> att = new set <string>(); set<string> evnt = new set <string>(); set<date> stdt = new set <date>(); set<date> endt = new set <date>(); //getting attendee, event, start and end dates in the event attendee records in the set defined above. for (event_attendee__c ea:trigger.new){ stdt.add(ea.event_start_date__c); endt.add(ea.event_end_date__C); att.add(ea.Attendee__c); evnt.add(ea.event1__C); } //querying for attendees that are a part of the trigger.new list list <attendee__C> attendee = [Select id from attendee__c where id in:att]; //querying for events (campaign is renamed to event1) that are a part of the trigger.new list list<campaign> event = [Select id from campaign where id in:evnt]; // querying the list of event attendees where the attendees, events, start date and end date is present in the trigger.new list list<event_attendee__C> evntovrlap1 = [SELECT id from event_attendee__C where attendee__c in: att and event1__C in: evnt and event_Start_Date__C in: stdt and event_End_Date__C in: endt]; //querying the list of event attendees where the start date is higher than the start date in trigger.new list //and end date is less than the end date in the trigger.new list. list<event_attendee__C> evntovrlap2 = [SELECT id from event_attendee__C where attendee__c in: att and event1__C in: evnt and event_Start_Date__C >=: stdt and event_end_Date__C <=: endt]; //querying the list of event attendees where the start date is less than the start date in trigger.new list //and end date is higher than the end date in the trigger.new list. list<event_attendee__C> evntovrlap3 = [SELECT id from event_attendee__C where attendee__c in: att and event1__C in: evnt and event_start_Date__C <=: stdt and event_end_Date__C >=: endt]; //for every attendee record and for every event attendee record, if attendee record finds a match in the event attendee record //and if the above counts are greater than 1 then, show an error message. for(attendee__c a : attendee){ for(campaign e : event){ for(event_attendee__c ea:trigger.new){ If(a.id==ea.Attendee__c && e.Id == ea.Event1__c){ If(evntovrlap1.size()>0 || evntovrlap2.size()>0 || evntovrlap3.size()>0){ ea.adderror('Attendee is already registered for these dates'); } } } } } }
-
This discussion was modified 5 years, 5 months ago by
Suman Humane.
-
This discussion was modified 5 years, 5 months ago by
Suman Humane.
-
This discussion was modified 5 years, 5 months ago by
Log In to reply.
Popular Salesforce Blogs
![[ Part - 1 ] G-Connector for Salesforce](https://images.forcetalks.com/wp-content/uploads/2018/05/25080906/Part-1-G-Connector-for-Salesforce.png)
G-Connector for Salesforce - Part 1
G-Connector for Salesforce It's a great tool or you can say an add-on that can help you to save your Salesforce Licenses. G-Connector is a…
![Marketing Challenges](https://images.forcetalks.com/wp-content/uploads/2022/04/18173703/Marketing-Challenges.jpg)
How Does Salesforce Marketing Cloud Resolves the Marketing Challenges of Small Businesses?
With time customer demand also increases, and it becomes a challenge for the marketer to create and maintain brand and product awareness. It becomes more…
Popular Salesforce Videos
Fillable PDFs for Salesforce Field Service
The ability to generate fillable PDFs for Salesforce Field Service can be a powerful tool. We demonstrate how to use PDFLib, a JavaScript library that…
Salesforce Vaccine Management | Salesforce Vaccine Cloud
Leveraging technology to manage vaccines has become increasingly important. Salesforce is investing in the public sector to aid the efforts to combat the spread of…
Get the Most Out of Your Digital Adoption Efforts On Your Salesforce Community with GuideIn
Navigating online communities can be tricky. GuideIn is a Salesforce-native, lightning-ready product from the house of Grazitti that enables you to maximize digital adoption in…