You need to sign in to do that
Don't have an account?
SeanCeno
Sum Total Completed Events on Contact Page
All,
I'm trying to create a field that totals all the completed events on a contact page. I think I'm on the right track, but I'm not sure how to go about making the event an integer, then sum all of them. This will eventually be a batch job that just runs every morning, but I have to get the class right first obviously. Any help would be great! Here's my code thus far:
I'm trying to create a field that totals all the completed events on a contact page. I think I'm on the right track, but I'm not sure how to go about making the event an integer, then sum all of them. This will eventually be a batch job that just runs every morning, but I have to get the class right first obviously. Any help would be great! Here's my code thus far:
public class TotalCompletedEvents { //Grab list of contacts protected final Contact[] contactNewList = new Contact[] {}; protected final Contact[] contactOldList = new Contact[] {}; public TotalCompletedEvents(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.Total_Completed_Events__c = null; } for(Event event : eventList) { Contact contact = contactMap.get(event.WhoId); if(Contact == null) continue; if(Event.EndDateTime < Date.Today()) Contact.Total_Completed_Events__c += Event.EndDateTime; } } }
//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;
}
}
All Answers
Thanks for the response. I added this code to my class and wrote this trigger, but it doesn't seem to update the field.
Trigger:
Class:
Am I supposed to use an integer instead of aggregateResult? Something like:
//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;
}
}
Does the trigger need to be on Event instead of Contact?
If anybody is having trouble with the trigger, it seems that you can't use a "Before Insert" with trigger.old. Removed the before insert and everything it fine.