+ Start a Discussion
arpit vijayvergiyaarpit vijayvergiya 

Trigger on Contact for Total Family Members

  • We have standard "Contact" object in SFDC.
  • You have to add one Field in Contact Object that is "Total Family Members" type is Number(2,0):
  • Let Account be the Family. So if there are 5 Contacts belongs to same account, that means these all 5 belongs to same family.
What you have to do:                 
  • Create a trigger on Contact to calculate "Family Members Count".
    • Create one Account: Create New Contact as a child for it. Then Contact's "Total Family Member" will have value 1.
    • Create another Contact for the same Account then Both Contact should have value 2 for "Total Family Member Field".
    • if someone changes contact's family like : changing account lookup value for a contact, then Both family will reflect. Because one family member is removed from one family and added to new Family. values should be updated: 
      •  All Contact's of Older Family should now have the "Total Family Member" value decreased by1. 
      • And for new family all Contact's "Total Family Member" will have value increased by1. 
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Arpit,

 

I think the field "Total Family Members" which you have created on Contact should be on Account which will show the count of Contacts corresponding to that Account.

And then if you want that count should be visible in each contact then create formula field on Contact which will store the value of "Total Family Members" from Account.

 

Below is the trigger:

 

trigger ContactCount on Contact (after insert, after update, after delete, after undelete) {
    Map<Id, List<Contact>> mapAcctIdContactList = new Map<Id, List<Contact>>();
    Map<Id, List<Contact>> mapAcctIdDelContactList = new Map<Id, List<Contact>>();
    Set<Id> AcctIds = new Set<Id>();    
    List<Account> listAcct = new List<Account>();
    
    if(trigger.isInsert) {
        for(Contact Con : trigger.New) {
            if(String.isNotBlank(Con.AccountId)) {
                if(!mapAcctIdContactList.containsKey(Con.AccountId)) {
                    mapAcctIdContactList.put(Con.AccountId, new List<Contact>());
                }
                mapAcctIdContactList.get(Con.AccountId).add(Con); 
                AcctIds.add(Con.AccountId);
            }   
        }  
    }
    
    if(trigger.isUpdate) {
        for(Contact Con : trigger.New) {
            if(String.isNotBlank(Con.AccountId) && Con.AccountId != trigger.oldMap.get(Con.Id).AccountId) {
                if(!mapAcctIdContactList.containsKey(Con.AccountId)){
                    mapAcctIdContactList.put(Con.AccountId, new List<Contact>());
                }
                mapAcctIdContactList.get(Con.AccountId).add(Con); 
                AcctIds.add(Con.AccountId);
            } else if(String.isBlank(Con.AccountId) && String.isNotBlank(trigger.oldMap.get(Con.Id).AccountId)) {
                if(!mapAcctIdDelContactList.containsKey(Con.AccountId)){
                    mapAcctIdDelContactList.put(Con.AccountId, new List<Contact>());
                }
                mapAcctIdDelContactList.get(Con.AccountId).add(Con);   
                AcctIds.add(trigger.oldMap.get(Con.Id).AccountId);
            }
        }  
    }
    
    if(trigger.isUndelete) {
        for(Contact Con : trigger.new) {
            if(String.isNotBlank(Con.AccountId)){
                if(!mapAcctIdContactList.containsKey(Con.AccountId)){
                    mapAcctIdContactList.put(Con.AccountId, new List<Contact>());
                }
                mapAcctIdContactList.get(Con.AccountId).add(Con);     
                AcctIds.add(Con.AccountId);
            }
        }  
    }      

    if(trigger.isDelete) {
        for(Contact Con : trigger.Old) {
            if(String.isNotBlank(Con.AccountId)){
                if(!mapAcctIdDelContactList.containsKey(Con.AccountId)){
                    mapAcctIdDelContactList.put(Con.AccountId, new List<Contact>());
                }
                mapAcctIdDelContactList.get(Con.AccountId).add(Con);    
                AcctIds.add(Con.AccountId); 
            }
        }  
    }   
    
    if(AcctIds.size() > 0) {
        listAcct = [SELECT Id, Total_Family_Members__c FROM Account WHERE Id IN : AcctIds];
        
        for(Account acct : listAcct) {
            Integer noOfConts = 0;
            if(mapAcctIdContactList.containsKey(acct.Id)) {
                noOfConts += mapAcctIdContactList.get(acct.Id).size();
            }
            if(mapAcctIdDelContactList.containsKey(acct.Id)) {
                noOfConts -= mapAcctIdDelContactList.get(acct.Id).size();
            }
            acct.Total_Family_Members__c = acct.Total_Family_Members__c == null ? noOfConts : (acct.Total_Family_Members__c + noOfConts);
        }
        
        update listAcct;    
    }
}
arpit vijayvergiyaarpit vijayvergiya
Hello Deepak,
thank  you
By your help i resolved the problem.
Here is code for that

trigger contactTrigger on Contact(after insert, after delete, after update, after undelete){
   
    Map<String, List<Contact>> mapOfAccContacts = new Map<String, List<Contact>>();
    Map<String, List<Contact>> mapAccIdDelCon = new Map<String, List<Contact>>();
    List<Contact> listOfContact = new List<Contact>();
    List<Contact> listOfUpdateContact = new List<Contact>();
    List<Account> lstAcc = new List<Account>();
    Set<String> setOfAccIds = new Set<String>();
    
    if(trigger.isInsert || trigger.isUndelete || trigger.isUpdate){
       for(Contact con : trigger.new){
           if(con.AccountId != null){
               setOfAccIds.add(con.AccountId);
           }
       }
    }
    if(trigger.isDelete || trigger.isUpdate){
       for(Contact con : trigger.old){
           if(con.AccountId != null){
               setOfAccIds.add(con.AccountId);
           }
       }
    }
    if(setOfAccIds.size()>0){
       listOfContact = [SELECT id,Total_Family_Members__c,accountId from Contact WHERE AccountId IN : setOfAccIds];
       if(listOfContact.size()>0){
           for(Contact objCon : listOfContact){
               List<Contact> templistOfContact = new List<Contact>();
               if(mapOfAccContacts.containsKey(objCon.accountId)){
                  templistOfContact = mapOfAccContacts.get(objCon.accountId);
               }
               templistOfContact.add(objCon);
               mapOfAccContacts.put(objCon.accountId,templistOfContact);
           }
           if(mapOfAccContacts.size()>0){
               for(Contact objCon : listOfContact){
                   if(objCon.Total_Family_Members__c != mapOfAccContacts.get(objCon.accountId).size()){
                       objCon.Total_Family_Members__c = mapOfAccContacts.get(objCon.accountId).size();
                       listOfUpdateContact.add(objCon);
                   }
               }
           }
       }
       if(listOfUpdateContact.size()>0){
           update listOfUpdateContact;
       }
    }
}
chandan kumar 99chandan kumar 99
Hello Gurus:-
My Scenario is same but I am creating a  Trigger is Before Event Using Aggregate Query But Field is not Update Proper
My Code Is:-
trigger Totalfamilymember2 on Contact (Before insert,Before update,Before delete) {
    set<id> ids = new set<id>();
    list<contact> contactToUpdate = new List<contact>();
    if(Trigger.isinsert||Trigger.isupdate){
        for(contact con : trigger.new){
            if(con.AccountId != null){
               Ids.add(con.AccountId);
           }
        }
    }
    
    if(Trigger.isdelete){
        for(contact con : trigger.old){
            if(con.AccountId != null){
               Ids.add(con.AccountId);
           }
        }
    }
    
     for(AggregateResult ar :[SELECT AccountId,count(id) related FROM Contact  where AccountId in: Ids  GROUP BY AccountId]){
                    
                 contactToUpdate.add(new contact(Accountid = (Id)ar.get('AccountId'),Total_Family_Member__c= (Decimal)ar.get('related') ));
           }
                
               
    
}
Ankit Bhati 20Ankit Bhati 20
Hi Arpit

Can you please explain why here is need Trigger.old and trigger.new when you just put same set of accountid  in Trigger.new and trigger.old