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
Raghavendra Sharma 12Raghavendra Sharma 12 

hi i am new in salesforce, i want to achieve roll up summary functionality through trigger, please help me

Best Answer chosen by Raghavendra Sharma 12
Saurabh Sood 12Saurabh Sood 12
This might be easy for u ..
have a look

All Answers

v varaprasadv varaprasad
Hi Raghavendra,

Please check once below links :

Hope this helps you.
Please let me know in case of any other assistance.


mukesh guptamukesh gupta
Hi Raghavendra,

i am using below object with rollup summary field 
Account (Parent Object)
Contact (Child Object).
Contact_Recs__c :--Roll up summary field

trigger CountNewContact on Contact (after insert) {

    List<id> accIdList = new List<id>();
    For(Contact con1 :{

    List<Account> accUpdateList = new List<Account>();
    For(Account acc : [SELECT Contact_Recs__c,(SELECT id FROM Contacts) FROM Account WHERE id =: accIdList]){
        acc.Contact_Recs__c = acc.Contacts.size();
        update accUpdateList;
    }Catch(Exception e){
        System.debug('Exception :'+e.getMessage());

if you need any help then let me know,

Please mark as BEST ANSWER !!!!!!

Hi Raghvendra,

I have handeled all the conditons of delete, insert, update. You can use below code as a sample for RollUpSummary trigger:
trigger childRollUp on Contact(after insert,after update, after delete, after undelete){
	set<ID> accountIds = New Set<ID>(); //Set to hold AccountId's
	List<Account> accountsToUpdate = New List<Account>(); //List to hold Accounts to update
	//To handle Insert operations
	for(Contact con : Trigger.New){
	//To Handle Update and Delete Operations
	if(Trigger.isUpdate || Trigger.isDelete){
		for(Contact conOld: Trigger.Old){
		Map<Id, Account> accountIdAndAccountMap = [Select id, RollUpField__c from Account where Id IN : accountIds];
		for(Account acc:[Select id, RollUpField__c, (Select id, AccountId from Contact) from Account where Id IN : accountIds]){
			accountIdAndAccountMap.get(acc.Id).RollUpField__c = acc.Contacts.Size();
		if(accountsToUpdate.Size() > 0){
			update accountsToUpdate;

Saurabh Sood 12Saurabh Sood 12
This might be easy for u ..
have a look
This was selected as the best answer