0

I am trying to create an EventRelation record on insert of an Event. Basically I am trying to add WhoID of Event into invitees automatically.

Am I missing anything. I am not getting any error also.

trigger EventTrigger on Event (before insert,after insert, after update, after delete) {
    if(!TriggerExecutionController.skipEventTrigger()) {  
        new EventTriggerHandler().run();     
    }

    public class EventTriggerHandler extends TriggerHandler {
        public map<Id, Event> newMap;
        public map<Id, Event> oldMap;
        public EventRecordSelector evntRecordSelector;

        public EventTriggerHandler() {
            this.newMap = (map<Id, Event>)Trigger.newMap;
            this.oldMap = (map<Id, Event>)Trigger.oldMap; 
        }

        public override void getRecordSelector() {
            evntRecordSelector = new EventRecordSelector(Trigger.new, this.oldMap, this);
        }

        public override void onAfterInsert() {
            if (!evntRecordSelector.setEventIds.isEmpty()) {
                EventUtilities.addEventAttendee(
                    evntRecordSelector.setEventIds
                ); 
            }
        }
    }

    public class EventRecordSelector implements RecordSelector {
        if(eveTriggerHandler.trigType == TriggerHandler.TriggerType.ISAFTER){
            if(
                   eveTriggerHandler.trigEvent == TriggerHandler.TriggerEvent.ISINSERT ||
                   eveTriggerHandler.trigEvent == TriggerHandler.TriggerEvent.ISUPDATE
            ){ 
                if(eventRecord.Id != null) {
                    setEventIds.add(eventRecord.Id);
                }
            }
        } 
    }

    public class EventUtilities {
        public static void addEventAttendee (set<Id> setNewEvents) {
            system.debug('@@@@@' + setNewEvents);
            List<Event> lstEvents = [
                SELECT Id,WhatId,WhoId 
                FROM Event 
                WHERE Id IN: setNewEvents
            ];

            for(Event e :lstEvents){
                EventRelation eR = new EventRelation();
                eR.EventId = e.Id;
                eR.RelationId = e.WhoId;
                system.debug('@@@@@' + eR);
                insert eR;
            }
        }
    }
}

1 Answer 1

1

The first thing I would say is that you have entirely too many layers of abstraction. This is a problem on a platform when CPU usage is a premium. This also makes troubleshooting issues much more difficult IMO (I'm sure others will have a different opinion however). I'm not going to say what you're doing wont work but I'd advise against it.

Also, you need to bulkify your trigger. Having an insert inside of a for loop is very much against best practices.

The following should work.

Utility Class

public class EventUtils {
    public static isTriggerEnabled = true;

    public static List<EventRelation__c> createEventRelations(List<Event> newEvents) {
        List<EventRelation__c> result = new List<EventRelation__c>();

        for (Event e : newEvents) {
            if (
                // logic to filter out only the events you care about
            ) {
                result.add(new EventRelation__c(
                    EventId__c = e.Id,
                    RelationId__c = e.WhoId
                ));
            }
        }

        return result;
    }
}

Trigger

trigger EventTrigger on Event (before insert, after insert, after update, after delete) {
    if (EventUtils.isTriggerEnabled) {
        switch on triggerEvent {
            when AFTER_INSERT{
                List<EventRelation__c> relations = EventUtils.createEventRelations(Trigger.New);

                if (!relations.isEmpty()) {
                    insert relations;
                }
            }
        }
    }
}
1
  • I am getting error ... Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>: [] Commented Aug 13, 2018 at 19:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .