function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Surender reddy SalukutiSurender reddy Salukuti 

Roolup summary using Trigger

Hi every one,

i have one scenario we wont to write trigger account and contact will have lookup relation ship if contacts will added automatically in the account object one custom field count will be increse (like lookup relation ship)

how can we acchive this if any one know please help me.
Thank you
Surender Reddy
9603757105
 
SEKAR RAJ.SEKAR RAJ.
Hi Surender Reddy,
1. You can use roll-up helper App Exchange product to count the number of contacts on Account.
https://appexchange.salesforce.com/appxListingDetail?listingId=a0N30000009i3UpEAI

2. You can write a trigger on contact to update the number of contacts on Account.
Trigger countContacts on Contact(After insert,After delete){
// logic goes here
}


Thanks,
SEKAR RAJ
Khan AnasKhan Anas (Salesforce Developers) 
Hi Surender,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger CountConOnAcc on Contact (after insert, after update, after delete) {
    
    Set <Id> accountIds = new Set <Id>();
    List <Account> lstAccountsToUpdate = new List <Account>();
    
    if(Trigger.isInsert){
        for(Contact con:trigger.new){
            accountIds.add(con.accountID);
        }
    }
    
    if(Trigger.isUpdate|| Trigger.isDelete){
        for(Contact con:trigger.old){
            accountIds.add(con.accountID);
        }
    }
    
    for(Account acc:[SELECT Id,Name,Number_Of_Contacts__c,(Select Id from Contacts) from Account where Id IN: accountIds]){
        Account accObj = new Account ();
        accObj.Id = acc.Id;
        accObj.Number_Of_Contacts__c = acc.Contacts.size();
        lstAccountsToUpdate.add(accObj);
    }
    
    if(lstAccountsToUpdate.size()>0){
        UPDATE lstAccountsToUpdate;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi Surender,
Try this code:
Trigger:
trigger CounntContact on Contact (after insert, after update) {
    if((trigger.IsAfter && trigger.IsInsert) || (trigger.IsAfter && trigger.IsUpdate) ) {
        CounntContact_Handler.incrementCount(trigger.new);
    }
}
Trigger Handler:
public class CounntContact_Handler {
    public static void incrementCount(List<Contact> conList) {
        try {
            if(conList.size () > 0) {
                Set <Id> accountIdSet = new Set <Id>();
                List <Account> accountList = new List <Account>();
                List <Contact> contactList = new List <Contact>();
                for(Contact con : conList) {
                    if(con.AccountId != null) {
                        accountIdSet.add(con.AccountId);
                    }
                }
                accountList = [Select Id, Number_Of_Contacts__c FROM Account WHERE Id IN : accountIdSet LIMIT 10000];
                contactList = [Select Id, AccountId FROM Contact LIMIT 10000];
                Integer temp;
                Map<Id, Integer> accountIdVsNoOfContact = new Map<Id, Integer>();
                for(Contact con1 : contactList) {
                    For(Contact con : conList) {
                        if(con1.AccountId == con.AccountId) {
                            if(!accountIdVsNoOfContact.containsKey(con1.AccountId)) {
                                accountIdVsNoOfContact.put(con1.AccountId, 1);
                            } else {
                                temp = accountIdVsNoOfContact.get(con1.AccountId);
                                temp++;
                                accountIdVsNoOfContact.put(con1.AccountId, temp);
                            }
                        }
                    }
                }
                
                system.debug('--accountIdVsNoOfContact---' + accountIdVsNoOfContact);
                For(Account acc : accountList) {
                    if(accountIdVsNoOfContact.containsKey(acc.Id)) {
                        acc.Number_Of_Contacts__c = accountIdVsNoOfContact.get(acc.Id);
                    }
                }
                update accountList;
            }
        }
        catch (Exception ex) {
            system.debug('Exception---ofLine--->'+ex.getLineNumber());
            system.debug('Exception---Message--->'+ex.getMessage());
        }
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi