You need to sign in to do that
Don't have an account?
Problem with an event´s trigger.
Greetings
Recently, I had to review a trigger that was made to avoid the overlapping of time when a new event is created. That trigger is not working (still is possible to do overlapping) I cant understand what is happening with that trigger. Please if anybody can help me with I will be grateful. The triiger is the following (was designed for the Event Object):
trigger DateCheckerTrigger on Event (before insert) {
for ( Event J : Trigger.New ) {
List<Event> eventList = [select id from event where ActivityDate= :J.ActivityDate and StartDateTime< :J.StartDateTime and EndDateTime> :J.StartDateTime and WhoId= :J.WhoId];
if(eventList.size()>0){
J.adderror('Ya existe una visita programada a la misma hora');
}
}
}
Thanks for your attention.
Best Regards
Your trigger is correct. its executing proper for me..only minor change you have to make is
[select id from event where ActivityDate= :J.ActivityDate and StartDateTime<= :J.StartDateTime and EndDateTime> =:J.StartDateTime and WhoId= :J.WhoId]
As the equal to operator is missing in the query. I guess its allowing you to create event in the same time slot. i tested and it worked fine.. cheers:)
@ Deepak.. As you said, Validation rule is a good idea, but i am not able to find few fields. so, i assume we cannot implement this using validation rule.. correct me if i am wrong.
Goodday,..keep learning..
@ Alcides Rojas 1 - Avoid query inside for loop.
Following Cases need to be included inside the query:-
(( StartDateTime >= :j.StartDateTime AND StartDateTime <= :j.StartDateTime ) OR ( EndDateTime >= :j.StartDateTime AND EndDateTime <= :j.EndDateTime) OR ( StartDateTime <= :j.StartDateTime AND EndDateTime >= :j.EndDateTime )) AND ActivityDate= :j.ActivityDate AND WhoId= :j.WhoId