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
Fiona BurnistonFiona Burniston 

Error Apex Trigger for Last Completed Event Date

Hi team,

I'm trying to capture the last completed event date on the contact object but I'm struggling with the trigger.

Here is the Apex Class - no errors

public class LastCompletedEventDate{
    //Grab list of contacts
    protected final Contact[] contactNewList = new Contact[] {};
        protected final Contact[] contactOldList = new Contact[] {};
            
            public LastCompletedEventDate(Contact[] contactOldList, Contact[] contactNewList) {
                this.contactNewList.addAll(contactNewList == null ? new Contact[] {} : contactNewList);
                this.contactOldList.addAll(contactOldList == null ? new Contact[] {} : contactOldList);
            }
    
    public void execute() {
        //find all events associated to contacts
        Event[] eventList = [select ActivityDate, WhoId, EndDateTime, Subject, ActivityDateTime from Event where WhoId in :contactNewList];
        
        Map<Id, Contact> contactMap = new Map<Id, Contact>(contactNewList);
        for(Contact contact : contactNewList) {
            contact.Last_Completed_Event__c = null;
        }
        
        //create if else statement to display most current completed event date
        for(Event event : eventList) {
            Contact contact = contactMap.get(event.WhoId);
            
            if(Event.EndDateTime < Date.Today())
                Contact.Last_Completed_Event__c = Event.EndDateTime;
        }
    }
}

Here is the trigger with the error 

trigger LastCompletedEventDate on Contact (before update) {
    if(EventID= true) {
        Set<ID> sID = new Set<ID>(trigger.newMap.keySet());
        new LastCompletedEventDate(trigger.old, trigger.new).execute();
    }
}

Any ideas on what I'm doing wrong?
Mahesh DMahesh D
Hi

what is EventID in your trigger?
and why are you capturing sId Set, you are not using it any where.

Regards,
Mahesh
Fiona BurnistonFiona Burniston
Hi Mahesh,

I thought I could use an IF statement for the trigger so if there is an event id assigned to the contact, this suggests and event is assigned to the contact.

Perhaps I need to start again on the trigger?

Thanks,
Fiona 
Mahesh DMahesh D
Please comment that EventId related if condition and sId. Try it now.
Fiona BurnistonFiona Burniston
Hi,

I'm struggling to write this trigger can you help?

thansk,
Fiona 
Mahesh DMahesh D
trigger LastCompletedEventDate on Contact (before update) {    
    LastCompletedEventDate lce = new LastCompletedEventDate(trigger.old, trigger.new);
    lce.execute();
}

Try this.

Regards,
Mahesh