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;
}
}
}
have a look
https://sfdssoru.blogspot.in/2017/12/roll-up-summary-fields-with-lookup.html
All Answers
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
i am using below object with rollup summary field
Account (Parent Object)
Contact (Child Object).
Contact_Recs__c :--Roll up summary field
if you need any help then let me know,
Please mark as BEST ANSWER !!!!!!
Regards
Mukesh
I have handeled all the conditons of delete, insert, update. You can use below code as a sample for RollUpSummary trigger:
Regards,
Ramakant
have a look
https://sfdssoru.blogspot.in/2017/12/roll-up-summary-fields-with-lookup.html