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
SeanCenoSeanCeno 

Trigger not reverting back to old values

Using the one trigger per object best practice, my trigger is not updating the results back to old values when an update (that would NOT be counted in the SOQL query) or delete occurs. Please see code below:

Class:
public with sharing class CompletedEvents {
    protected final Event[] eventOldList;
    protected final Event[] eventNewList;
    Set<Id> contIds = new Set<Id>();

    public CompletedEvents(Event[] eventOldList, Event[] eventNewList) {
        this.eventOldList = eventOldList;
        this.eventNewList = eventNewList;
    }

    public CompletedEvents(List<Event> events){
        this.eventNewList = events;
    }
   
 public void executeTotalConferenceCalls(){
        for(Event e : eventNewList){
            if(e.WhoId != null){
                contIds.add(e.WhoId);
            }
        }

        AggregateResult [] ars = [Select WhoId eventId, count(id) ConfCount
                                  From Event
                                  Where WhoId in :contIds
                                  AND EndDateTime < :date.today()
                                  AND isDeleted = False
                                  AND Subject = 'Conference Call'
                                  GROUP BY WhoId All Rows];

        List<Contact> updCon = new List<Contact>();
        for(AggregateResult ar : ars){
            Contact c = new Contact();
            c.Id = (Id)ar.get('eventId');
            c.Total_Conference_Calls__c = Integer.ValueOf(ar.get('ConfCount'));
            updCon.Add(c);
        }

        if(updCon.size()>0){
            update updCon;
        }
    }

    public void executeLastCompletedEvent(){
        for(Event e : eventNewList){
            if(e.WhoId != null){
                contIds.add(e.WhoId);
            }
        }

        Event[] eventList = [Select WhoId, Subject, EndDateTime, ActivityDateTime, ActivityDate
                             From Event
                             Where WhoId in :contIds
                             AND EndDateTime < :date.today()
                             AND isDeleted = False
                             AND Subject != 'B/D-Conference'
                             AND Subject != 'Cancel/No Show'
                             AND Subject != 'DD Meeting'
                             AND Subject != 'NRS-Conference'
                             AND Subject != 'Other'
                             AND Subject != 'Drop-In'
                             AND Subject != 'Business Review'
                             AND Subject != 'One-On-One'
                             AND Subject != 'Travel'
                             AND Subject != 'Professional Association Event'
                             ORDER BY EndDateTime ASC All Rows];

        Map<Id, Contact> cMap = new Map<Id, Contact>();
        for(Event e : eventList){
            Contact c = new Contact(Id = e.WhoId);
            if(c.Id != null){
                c.Last_Completed_Event__c = e.EndDateTime;
                cMap.put(c.Id, c);
            }
        }

        update cMap.values();
    }
}

Trigger
trigger MasterEventTrigger on Event (
    before insert, after insert,
    before update, after update,
    before delete, after delete){
    Event[] eventOldList = trigger.IsDelete ? null : trigger.old;
    Event[] eventNewList = trigger.IsDelete ? trigger.old : trigger.new;
        
    if (Trigger.isBefore) {
        if (Trigger.isInsert) {
                new CompletedEvents(eventOldList, eventNewList).executeTotalConferenceCalls();
                new CompletedEvents(eventOldList, eventNewList).executeLastCompletedEvent();
        }

        if (Trigger.isUpdate) {
                new CompletedEvents(eventOldList, eventNewList).executeTotalConferenceCalls();
                new CompletedEvents(eventOldList, eventNewList).executeLastCompletedEvent();
        }

        if (Trigger.isDelete) {
                new CompletedEvents(eventOldList, eventNewList).executeTotalConferenceCalls();
                new CompletedEvents(eventOldList, eventNewList).executeLastCompletedEvent();
        }
    }
    
    if (Trigger.IsAfter) {
        if (Trigger.isInsert) {
        }
        if (Trigger.isUpdate) {
        }
        if (Trigger.isDelete) {
        }
    }
}

 
Alain CabonAlain Cabon
Hi,

Why and when do you want to revert back to old values?   

You are only working with the new values (eventNewList) in executeTotalConferenceCalls() and executeLastCompletedEvent().
 
SeanCenoSeanCeno
If the Event is updated to one of the Subject values I'm trying to exclude in the SOQL query or no longer equals 'Conference Call' in the Aggregate Result. Also, if the event is deleted, it should change the field value back to its original value.
Alain CabonAlain Cabon
Ok. I missed 

Event[] eventOldList = trigger.IsDelete ? null : trigger.old;
Event[] eventNewList = trigger.IsDelete ? trigger.old : trigger.new;

That would need more time to further reflect on it.
SeanCenoSeanCeno
Yeah I'm not sure if the trigger.old logic needs to be in the methods themselves or not.
Antal Godkewitsch 1Antal Godkewitsch 1
You are querying the database before the change occured.
I think you should do your processing in the Trigger.IsAfter branch of your code. 
SeanCenoSeanCeno
Thanks, Antal. Unfortunately this also didn't work. I believe there is some sort of logic I'm missing in the methods themselves.
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
@Sean - Does the record get updated by workflow in the middle of execution ? if that is the case then trigger.old will contain initial update values but not new ones.