Thursday, November 30, 2017

Trigger to count events related to an Account

The following trigger will count the number of events related to an account.
The trigger will execute whenever an event is created/updated/deleted and is related to account.

trigger EventCount on Event (after insert, after update, after delete, after undelete) {
    Map<Id, List<Event>> mapAcctIdEventList = new Map<Id, List<Event>>();//Map to maintain new events for an account
    Map<Id, List<Event>> mapAcctIdDelEventList = new Map<Id, List<Event>>();//Map to maintain deleted events for an account
    Set<Id> AcctIds = new Set<Id>();   
    List<Account> listAcct = new List<Account>();
   //If an event is inserted and is related to account
    if(trigger.isInsert) {
        for(Event eve : trigger.New) {
            if(String.isNotBlank(eve.WhatId)) {
                Id WhatIdObj = eve.WhatId;
                if(whatIdObj.getsObjectType() == Account.sObjectType)
                {
                    if(!mapAcctIdEventList.containskey(eve.WhatId)) {                   
                        mapAcctIdEventList.put(eve.WhatId, new List<Event>());
                    }
                    mapAcctIdEventList.get(eve.WhatId).add(eve);
                    AcctIds.add(eve.WhatId);
                }
            }         
        } 
    }
   
//If an event is updated  1) If RelatedTo is changed from one account to other account 2)If RelatedTo is previously populated and changed to blank.
    if(trigger.isUpdate) {
        for(Event eve : trigger.New) {
            if(String.isNotBlank(eve.WhatId) && eve.WhatId != trigger.oldMap.get(eve.Id).WhatId) {
                Id WhatIdObj = eve.WhatId;
                if(whatIdObj.getsObjectType() == Account.sObjectType)
                {
                    if(!mapAcctIdEventList.containskey(eve.WhatId)){
                        mapAcctIdEventList.put(eve.WhatId, new List<Event>());
                    }
                    mapAcctIdEventList.get(eve.WhatId).add(eve);
                    AcctIds.add(eve.WhatId);
                    if(trigger.oldMap.get(eve.Id).WhatId!=null)
                    {
                        if(!mapAcctIdDelEventList.containskey(trigger.oldMap.get(eve.Id).WhatId)){
                            mapAcctIdDelEventList.put(trigger.oldMap.get(eve.Id).WhatId, new List<Event>());
                        }
                        mapAcctIdDelEventList.get(trigger.oldMap.get(eve.Id).WhatId).add(eve); 
                        AcctIds.add(trigger.oldMap.get(eve.Id).WhatId);
                    }
                }             
            } else if(String.isBlank(eve.WhatId) && String.isNotBlank(trigger.oldMap.get(eve.Id).WhatId)) {
                Id WhatIdObj = eve.WhatId;
                if(whatIdObj.getsObjectType() == Account.sObjectType)
                {
                    if(!mapAcctIdDelEventList.containskey(eve.WhatId)){
                        mapAcctIdDelEventList.put(trigger.oldMap.get(eve.Id).WhatId, new List<Event>());
                    }
                    mapAcctIdDelEventList.get(trigger.oldMap.get(eve.Id).WhatId).add(eve); 
                    AcctIds.add(trigger.oldMap.get(eve.Id).WhatId);
                }
            }
        } 
    }
   //If an event is undelete an event is added to account 
    if(trigger.isUndelete) {
        for(Event eve : trigger.new) {
            if(String.isNotBlank(eve.WhatId)){
                Id WhatIdObj = eve.WhatId;
                if(whatIdObj.getsObjectType() == Account.sObjectType)
                {
                    if(!mapAcctIdEventList.containskey(eve.WhatId)){
                        mapAcctIdEventList.put(eve.WhatId, new List<Event>());
                    }
                    mapAcctIdEventList.get(eve.WhatId).add(eve);   
                    AcctIds.add(eve.WhatId);
                }
            }
        } 
    }     
    //If an event is deleted
    if(trigger.isDelete) {
        for(Event eve : trigger.Old) {
            if(String.isNotBlank(eve.WhatId)){
                Id WhatIdObj = eve.WhatId;
                if(whatIdObj.getsObjectType() == Account.sObjectType)
                {
                    if(!mapAcctIdDelEventList.containskey(eve.WhatId)){
                        mapAcctIdDelEventList.put(eve.WhatId, new List<Event>());
                    }
                    mapAcctIdDelEventList.get(eve.WhatId).add(eve);   
                    AcctIds.add(eve.WhatId);
                }
            }
        } 
    } 
   
    if(AcctIds.size() > 0) {
        listAcct = [SELECT Id, Activity_Count__c FROM Account WHERE Id IN : AcctIds];
       
        for(Account acct : listAcct) {
            Integer noOfevets = 0;
            if(mapAcctIdEventList.containskey(acct.Id)) {
                noOfevets += mapAcctIdEventList.get(acct.Id).size();
            }
            if(mapAcctIdDelEventList.containskey(acct.Id)) {       
                noOfevets -= mapAcctIdDelEventList.get(acct.Id).size();       
            }
            acct.Activity_Count__c = acct.Activity_Count__c == null ? noOfevets : (acct.Activity_Count__c + noOfevets);
                }
       
        update listAcct;   
    }
}