Is it possible to create a rollup summary field on Contact to count the activities. For example, count the total number of times 'Log a call', total number of times 'log a meeting', and total number of times 'Log an email'?
1 Answer
Rollup summary fields can only be used on Master-Detail relationships where you wish to rollup child information to the parent i.e detail to master.
In this case, you need to use Apex. A declarative tool like Flow is possible but to ensure you are working from the correct Related To Sobject
, you will need to invoked Apex.
Sample Apex:
List<Id> conIds = new List<Id>();
for (Task t : Trigger.New) {
if ((t.WhoId.getSObjectType() == Contact.sObjectType) && t.Subject == 'Log a Call') {
conIds.add(t.WhoId);
}
}
if (conIds.size() > 0) {
List<Contact> cons = [select id, CallLogs__c from Contact where id in :conIds];
for (Contact con : cons) {
if (con.CallLogs__c == null) con.CallLogs__c = 1;
else con.CallLogs__c += 1;
}
update cons;
}
A custom Number field has been added to the Contact object to count the specific number of "Log a Call" Tasks. You may wish to aggregate all Task Subjects in a single field, thats a design choice on your side. Another design decision for you is to build a known of set of Task subjects to check against. Note the check on WhoId.getSObjectType()
as Task is a shared object and you want to safe guard your code for later operations.
You should extend this Apex to accommodate delete
operations if you wish to decrement the count on the Contact record.