+ Start a Discussion
SeanCenoSeanCeno 

Last Completed Event Date

All,
I wrote a class and trigger to create a field that would display the most recent completed event for a Contact. I thought it worked well, but there seems to be one problem. If the Contact doesn't have a previous completed event and a User schedules a new event for sometime in the future, the field updates to the future Due Date. 

The line causing the problem is as follows:
if(Contact.Last_Completed_Event__c == null)
           Contact.Last_Completed_Event__c = Event.EndDateTime;

Now if I comment this out, it won't grab a future Due Date, but it also won't grab the most recent completed event if an OPEN event doesn't exist. Can somebody help me solve this so that it works properly?
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(contact == null)
                continue;
            //if(Contact.Last_Completed_Event__c == null)
                //Contact.Last_Completed_Event__c = Event.EndDateTime;
            if(Contact.Last_Completed_Event__c == null && Event.EndDateTime < Date.Today())
                Contact.Last_Completed_Event__c = Event.EndDateTime;
            if(Event.EndDateTime < Date.Today())
                Contact.Last_Completed_Event__c = Event.EndDateTime;
        }
    }
}
trigger LastCompletedEventDate on Contact (before update) {
    if(Contact_RollupTrades.inprog != true) {
        Set<ID> sID = new Set<ID>(trigger.newMap.keySet());
        new LastCompletedEventDate(trigger.old, trigger.new).execute();
    }
}
Best Answer chosen by SeanCeno
SeanCenoSeanCeno
Never mind. I'm an idiot. Below code contains the correct IF statement. Much simpler.
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;
        }
    }
}


 

All Answers

SeanCenoSeanCeno
Never mind. I'm an idiot. Below code contains the correct IF statement. Much simpler.
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;
        }
    }
}


 
This was selected as the best answer
Stephen MaderStephen Mader
Hey Sean, 

This is great stuff! I am a certified admin that does NOT have the same skill set you clearly have. How would I do this for opportunities?