+ Start a Discussion
Rabbani sayyed 8Rabbani sayyed 8 

synario: If I don’t have a master detail relationship from parent to child one, then how can I count no. of child records on parent one? any suggestions on this?

Narsimulu ChiranjiNarsimulu Chiranji
you need to write trigger on parent 
sfdc Beginnersfdc Beginner
Hi Sayyad,

Consider two Objects Account and Contact, here Account is Parent Object and Contact is Child Object.

Create a  custom field called as Number of Contacts on Account Objects to store the number of Contacts.

Then write a Trigger on Contact Object to display the count of contacts on Account Records.

Here is the Code.
 
trigger CountsContact on Contact (after insert,after update,after delete,after undelete) {
  
    set<Id> accId = new set<Id>();
    
    if(Trigger.isInsert || Trigger.isUndelete){
        for(Contact con : Trigger.new){
            accId.add(con.accountId);
        }
    
    
    List<Account> accounts = [SELECT Id,Number_of_Contacts__c FROM Account WHERE Id IN :accId];
    
    List<Contact> contacts = [SELECT Id FROM Contact WHERE AccountId IN :accId ];
    
    for(Account a: accounts ){
        
        a.Number_of_Contacts__c = contacts.size();
        
    }
    
    update accounts;
    }     
    
    if(Trigger.isDelete){
        
        for(Contact con : Trigger.old){
          accid.add(con.accountId);
        }
        
        List<Account> accounts = [SELECT Id,Number_of_Contacts__c FROM Account WHERE Id IN :accId];
    
      List<Contact> contacts = [SELECT Id FROM Contact WHERE AccountId IN :accId ];
        
        for(Account a: accounts){
            
            a.Number_of_Contacts__c = contacts.size();
        }
        
        update accounts;
        
    }
    
    
    if(Trigger.isUpdate){
        
        set<Id> oldAccId = new Set<Id>();
        
        for(Contact con : Trigger.new){
            
            if(con.AccountId != Trigger.oldMap.get(con.id).AccountId) {
                accId.add(con.AccountId); 
              OldAccId.add(Trigger.oldMap.get(con.id).AccountId); 
            }

            if(!accId.isEmpty()){
                
                // for new Accounts
                
                List<Account> newaccounts = [SELECT Id,Number_of_Contacts__c FROM Account WHERE Id IN : AccId];
                
                // For New Account Contacts
                
                List<Contact> newcontacts = [SELECT Id FROM Contact WHERE AccountId IN :AccId];
                
                
                // This is for Old Contacts Count
                
                // For Old Accounts
                
                List<Account> OldAccounts = [SELECT Id,Number_of_Contacts__c FROM Account WHERE Id IN : OldAccId];
                
                // For Old Account Contacts
                
                List<Contact> OldContacts = [SELECT Id FROM Contact WHERE AccountId IN :OldAccId];
                
                // For New Accounts
                
                for(Account a : newaccounts){
                    a.Number_of_Contacts__c = newcontacts.size();
                }
                
                Update newaccounts;
                
                
                // For Old Contacts
                
                for(Account a: oldAccounts){
                    a.Number_of_Contacts__c = oldAccounts.size(); 
                }
                
                Update oldAccounts;
                
                
            }
        }
    
    }
}


Thanks,
SFDC Beginner.



Note: If my answer helps you solve your Problem, Please Mark it as the Best Answer.