+ Start a Discussion
Mitch MorrisonMitch Morrison 

Apex trigger changing event value from contact field change - NOT WORKING

Hello, I have a trigger that works for my test class, but in implementation, it doesn't work. When the field "YTD_sales" changes on a contact, the trigger should increment the field "Sales within 1 Year" for every event tied to the contact.
Right now, only some event values get updated, and it is especially problematic for multi-person events (which I tried addressing through event relations). The batch size of the import could be an issue (its 10,000)
Let me know if you have any thoughts! Thanks!
(I'll post the code below)
 
Mitch MorrisonMitch Morrison
PART 1
Trigger: 
trigger EventSales on Contact (before update) {
    //Create a list of contact IDs that have updating sales
    List<Id> contactIds = new List<Id>();
    System.debug('Contact Ids List Created');
    for(Contact con : trigger.new) {
        if(trigger.newMap.get(con.Id).YTD_Sales__c == NULL) {
            System.debug('New YTD Sales is NULL');
        }
        else if(trigger.newMap.get(con.Id).YTD_Sales__c == 0) {
            System.debug('New YTD Sales is 0');
        }
        else if(trigger.oldMap.get(con.Id).YTD_Sales__c == NULL) {
            System.debug('Old YTD Sales is NULL');
            if(contactIds.contains(con.Id)) {
                System.debug('Contact Id already on list (1)');
            }
            else {
                contactIds.add(con.Id);
                System.debug('Adding Contact Id to list (1)');
            }
        }
        else if(trigger.newMap.get(con.Id).YTD_Sales__c !=
                trigger.oldMap.get(con.Id).YTD_Sales__c) {
            System.debug('YTD Sales dont match');
            if(contactIds.contains(con.Id)) {
                System.debug('Contact Id already on list (2)');
            }
            else {
                contactIds.add(con.Id);
                System.debug('Adding Contact Id to list (2)');
            }
        }
        else System.debug('Loop not finished');
    }
    //Create a list of event relations (contact-event pairs) for updating contacts
    List<EventRelation> evRel = [SELECT EventId, RelationId
                                FROM EventRelation
                                WHERE RelationId IN :contactIds];
    System.debug('Event Relation List query ran');
   
    //Create a map of the updating contacts and their related events
    Map<Id, List<Id>> contactEvents = new Map<Id, List<Id>>();
    System.debug('Contact events map created');
   
    //Create a list of event Ids that are going to be updated
    List<Id> eventIds = new List<Id>();
    System.debug('Event Ids List Created');
   
    //Put the contact Id and event Id in the map for events that have updating contacts
    for(EventRelation rel :evRel) {
        if(contactEvents.containsKey(rel.RelationId)) {//If the contact is already in the map
            System.debug('Relation Id is in the map');
            List<Id> relatedEv = contactEvents.get(rel.RelationId);//Grab the list of events tied to the contact
            if(relatedEv.contains(rel.EventId)) {
                System.debug(relatedEv);
                System.debug(contactEvents);
                System.debug('Event already in contacts list in map');
            }
            else {
                System.debug(relatedEv);
                relatedEv.add(rel.EventId);//Add the new event to the list
                System.debug(relatedEv);
                System.debug('Added event to contacts list in map');
                contactEvents.put(rel.RelationId, relatedEv);//Put the updated list into the map
                System.debug(contactEvents);
                System.debug('Added contact-event pair to map (1)');
            }
            if(eventIds.contains(rel.EventId)) {
                System.debug(eventIds);
                System.debug('Event Id is already in list (1)');
            }
            else {
                System.debug(eventIds);
                eventIds.add(rel.EventId);
                System.debug(eventIds);
                System.debug('Event Id added to list (1)');
            }
        }
        else {//If the contact isn't in the map
            System.debug('Contact (relation ID) is not in list');
            contactEvents.put(rel.RelationId, new List<Id>{rel.EventId});//Put the contact and event into the map
            System.debug('Added contact-event pair to map (2)');
            if(eventIds.contains(rel.EventId)) {
                System.debug('Event Id is already in list (1)');
            }
            else {
                eventIds.add(rel.EventId);
                System.debug('Contact (relation ID) is not in list (1)');
            }
        }
    }
   
    //Create a list of events that are going to be updated
    List<Event> listEventsMaker = [SELECT Id, Sales_within_1_year__c
                                   FROM Event
                                   WHERE Id IN :eventIds
                                   AND EndDateTime = LAST_N_DAYS:365];
    System.debug('Event list maker query ran');

    List<Event> listEvents = new List<Event>();
    System.debug('Event list created');
   
    for(Event ev :listEventsMaker) {
        if(listEvents.contains(ev)) {
            System.debug('Event in list already');
        }
        else {
            listEvents.add(ev);
            System.debug('Added event to list');
        }
    }
  
Mitch MorrisonMitch Morrison
    //Keep a list of events to update
    List<Event> changedEvents = new List<Event>();
    System.debug('Changed events list created');
   
    //Update the sales amounts for the events
    for(Id con :contactEvents.keySet()) {//For each contact in the map
        List<Id> thisContact = new List<Id>();//Create a list of events tied to this specific contact
        System.debug('This contact list created');
        Contact oldCon = trigger.oldMap.get(con); //create a record for the contact pre-update
        Contact newCon = trigger.newMap.get(con); //create a record for the contact post-update
        if(newCon.YTD_Sales__c == NULL) {
            System.debug('New YTD sales is NULL');
        }
        else if(oldCon.YTD_Sales__c
                >= newCon.YTD_Sales__c) {
            System.debug('New YTD sales is less than or equal to old YTD sales');
        }
        else if(oldCon.YTD_Sales__c == NULL) {
            System.debug('Old YTD sales is NULL');
            for(Id event :contactEvents.get(con)) {
                if(thisContact.contains(event)) {
                    System.debug('Event already on this contact list (1)');
                }
                else {
                    thisContact.add(event);
                    System.debug('Event added to this contact list (1)');
                }
            }
            for(Event ev :listEventsMaker) {
                if(thisContact.contains(ev.Id)) {
                    System.debug('Event is in this contact list');
                    if(ev.Sales_within_1_Year__c == NULL) {
                        System.debug('Event sales is NULL (1)');
                        ev.Sales_within_1_Year__c = newCon.YTD_Sales__c;
                        System.debug('Set event sales equal to new YTD sales');
                    }
                    else {
                        System.debug('Event sales is not NULL (1)');
                        ev.Sales_within_1_Year__c += newCon.YTD_Sales__c;
                        System.debug('Increment event sales (1)');
                    }
                    if(changedEvents.contains(ev)) {
                        System.debug('Event already on changed events list (1)');
                    }
                    else {
                        changedEvents.add(ev);
                        System.debug('Added event to changed events list (1)');
                    }
                }
            }
        }
        else if(oldCon.YTD_Sales__c
                < newCon.YTD_Sales__c) {
            System.debug('New YTD sales is greater than old YTD sales');
            for(Id event :contactEvents.get(con)) {
                if(thisContact.contains(event)) {
                    System.debug('Event already on this contact list (2)');
                }
                else {
                    thisContact.add(event);
                    System.debug('Event added to this contact list (2)');
                }
            }
            for(Event ev :listEventsMaker) {
                if(thisContact.contains(ev.Id)) {
                    System.debug('Event is in this contact list');
                    if(ev.Sales_within_1_Year__c == NULL) {
                        System.debug('Event sales is NULL (2)');
                        ev.Sales_within_1_year__c = (newCon.YTD_Sales__c
                                                     - oldCon.YTD_Sales__c);
                        System.debug('Increment event sales (2)');
                    }
                    else {
                        System.debug('Event sales is not NULL (2)');
                        ev.Sales_within_1_Year__c += (newCon.YTD_Sales__c
                                                       - oldCon.YTD_Sales__c);
                        System.debug('Increment event sales (3)');
                    }
                    if(changedEvents.contains(ev)) {
                        System.debug('Event already on changed events list (2)');
                    }
                    else {
                        changedEvents.add(ev);
                        System.debug('Added event to changed events list (2)');
                    }
                }
            }
        }
    }
    update changedEvents;
    System.debug('Events updated');  
}