+ Start a Discussion

Trigger to look for Event already scheduled for user.


Hi There,


I have the trigger bellow working fine, but now I need to check, before the trigger updates the Lead from an Event, If the Event being created/updated has a "Start Date" that will match another possible event, already scheduled for the same user (Assigned to) with the same "Start Date". It should not let the user create the event and an Alert should come up saying that "there is another Event already scheduled for you on that same date/time".


Here is the Trigger:


trigger tourchangeschedule on Event (before insert, after update) {
    Set<Id> recordIds = new Set<Id>();
    Set<id> eventidhold = New Set<id>();
    Set<Id> tourownerid = new Set<Id>();
    for ( Event  t : Trigger.New ) {
        if(t.subject=='tour' && t.Stages__c <> '30 days' && t.Stages__c <> '60 days'){

    List<lead> leadsOfEvents = [select id from lead where id in :recordIds];

    Map < Id ,lead > leadmap = new Map < Id , lead >();

    for ( lead  l : leadsOfEvents   ) {
        leadmap.put( l.Id, l);

    List < lead > leadsToUpdate = new List < lead >();
    for(Event e: Trigger.New) {
        lead lc = leadmap.get( e.WhoID );
        if ( lc == null ) {
        lc.sales_cycle__C = 'Tour Scheduled';
        lc.tour_scheduled__C = e.StartDateTime;

    upsert leadsToUpdate;       



I though i adding the list:  List <event> toursbooked = [Select id from event where ownerid in :tourownerid and startdatetime = e.startDateTime]


IF "toursbooked" finds a match then "You already have a n event scheduled for the date/time"



Proceed with the lead update as the code above, starting at "List<lead> leadsOfEvents..."



Thanks fot the help!