+ Start a Discussion
SalesForce Administrator 153SalesForce Administrator 153 

Create a custom lead field that will sum up the count of activities logged

We are in need of a custom field to be created on our leads page that will auto-sum the number of logged calls for that particular lead. I connected with the basic SalesForce HelpDesk but they have said due to several custom objects that they are unable to process this request.
ManojSankaranManojSankaran
Hi,

This can be achieved by two ways.
1. Using Process Builder + Flow
2. Using a simple trigger in Activity.

Below are the steps for creating a trigger in activity object.
1. Create a number field (Activity Count) in Lead Object
2. Create a trigger in Task Object and below is the code

After trigger (after insert)
Set<Id> LeadIds = new Set<Id>();
Map<id,integer> LeadMap = new Map<id,integer>();

for(Task t : Trigger.New){
if(t.whoid.startswith('005')){
      integer ActivityCount = 1;
      if(LeadMap.containsKey(t.whoId)){
            ActivityCount = ActivityCount + LeadMap.get(t.whoId);
      }
      LeadMap.put(t.whoId,ActivityCount);
}
}
List<Lead> LeadList = new List<Lead>();
for(Lead l : [select id,Activity_Count__c from Lead where id : LeadMap.Keyset()]){
      l.Activity_Count__c = LeadMap.get(l.id);
LeadList.add(l);
}


update LeadList;
Mark AdamoMark Adamo
Hi ManojSankaran I get this error when I use your code Missing '<EOF>' at 'After'