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
Alcides Rojas 1Alcides Rojas 1 

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

Deepak GulianDeepak Gulian
You can achieve this easily by Event validation rule!, no need to write down a trigger
gayatri sfdcgayatri sfdc
Hey Rojas,

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..
 
Deepak GulianDeepak Gulian
@ gayatri sfdc - Yes you are right, actually I misunderstood the question in first time. Comparison with other reocrds is there , so there is no chance for validation rule.
@ 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