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 

Access Archived Events Using queryAll() in SOQL

All,
Our Salesforce org history is set to 24 months. I recently created some code to sum "Total Completed Events" and although I can still see completed Events older than 24 months on a Contact page, it doesn't include them to the sum. I read up on queryAll(), but I'm not sure how to use it in my current SOQL. Is there a way to grab older events since inception using queryAll() that also won't grab deleted events from the recycle bin? Here is my code if that helps:

Class
public class TotalCompletedEvents {
    //Grab list of contacts
    protected final Contact[] contactNewList;
    protected final Contact[] contactOldList;
        
    public TotalCompletedEvents(Contact[] contactOldList, Contact[] contactNewList) {
        this.contactNewList = contactNewList;
        this.contactOldList = contactOldList;
    }
    public void execute() {
        Map<Id, Contact> contactMap = new Map<Id, Contact>(contactNewList);
        AggregateResult[] ars = [select count(id),whoid from event where EndDateTime < :date.today() and whoid in :contactNewList group by whoid];
        Map<Id, Contact> contactMap1 = new Map<Id,Contact>(contactNewList);
        for (AggregateResult ar : ars) {
            //System.debug('Total Completed Events: ' + ar.get('expr0'));  //get count()
            //System.debug('Contact ID' + ar.get('whoid'));  //get id of contact
            Contact c = contactMap.get((id)ar.get('whoid'));
            c.total_completed_events__c = integer.valueof(ar.get('expr0'));
            contactMap1.remove((id)ar.get('whoid'));
        }
        for (Contact c : contactMap1.values()){
            c.total_completed_events__c = 0;
        }
        //update ContactNewList;
    }
}

Trigger
trigger TotalCompletedEvents on Contact (before update) {
    Contact[] contactOldList = trigger.IsDelete ? null : trigger.old;
    Contact[] contactNewList = trigger.IsDelete ? trigger.old : trigger.new;
    new TotalCompletedEvents(contactOldList, contactNewList).execute();
}

 
Best Answer chosen by SeanCeno
SeanCenoSeanCeno
If anybody is looking for the answer to this, the key is to use ALL ROWS in the SOQL. Also, if you don't want deleted events, add 'isDeleted = False' to the query. The working code is below.
 
public class TotalCompletedEvents {
    //Grab list of contacts
    protected final Contact[] contactNewList;
    protected final Contact[] contactOldList;
        
    public TotalCompletedEvents(Contact[] contactOldList, Contact[] contactNewList) {
        this.contactNewList = contactNewList;
        this.contactOldList = contactOldList;
    }
    public void execute() {
        Map<Id, Contact> contactMap = new Map<Id, Contact>(contactNewList);
        AggregateResult[] ars = [select count(id),whoid from event where EndDateTime < :date.today() and whoid in :contactNewList AND isDeleted = False group by whoid All Rows];
        Map<Id, Contact> contactMap1 = new Map<Id,Contact>(contactNewList);
        for (AggregateResult ar : ars) {
            //System.debug('Total Completed Events: ' + ar.get('expr0'));  //get count()
            //System.debug('Contact ID' + ar.get('whoid'));  //get id of contact
            Contact c = contactMap.get((id)ar.get('whoid'));
            c.total_completed_events__c = integer.valueof(ar.get('expr0'));
            contactMap1.remove((id)ar.get('whoid'));
        }
        for (Contact c : contactMap1.values()){
            c.total_completed_events__c = 0;
        }
        //update ContactNewList;
    }
}