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 
https://sfdssoru.blogspot.in/2017/12/roll-up-summary-fields-with-lookup.html

All Answers

v varaprasadv varaprasad
Hi Raghavendra,

Please check once below links : 

http://blog.jeffdouglas.com/2009/07/30/roll-up-summary-fields-with-lookup-relationships-part-1/
https://developer.salesforce.com/forums/?id=906F00000008yWuIAI



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

Thanks
Varaprasad






 
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 : Trigger.new){
      accIdList.add(con1.accountid);
    }
   


    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();
        accUpdateList.add(acc);
    }
    try{
        update accUpdateList;
    }Catch(Exception e){
        System.debug('Exception :'+e.getMessage());
    }
}

if you need any help then let me know,

Please mark as BEST ANSWER !!!!!!

Regards
Mukesh
RKSalesforceRKSalesforce
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){
		accountIds.add(con.AccountId);
	}
	//To Handle Update and Delete Operations
	if(Trigger.isUpdate || Trigger.isDelete){
		for(Contact conOld: Trigger.Old){
			accountIds.add(conOld.AccountId);
		}
	}
	if(!accountIds.isEmpty()){
		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();
			accountsToUpdate.add(acc);	
		}
		if(accountsToUpdate.Size() > 0){
			update accountsToUpdate;
		}
	}
}

Regards,
Ramakant​
Saurabh Sood 12Saurabh Sood 12
This might be easy for u ..
have a look 
https://sfdssoru.blogspot.in/2017/12/roll-up-summary-fields-with-lookup.html
This was selected as the best answer