Is there anyway to update a rollup summary field with lookup relation ship for a custom object. How can we achieve this using triggers ?
-
Sorry, I've deleted my answer, because I misunderstood the question. Here you have a good article to follow: blog.jeffdouglas.com/2009/07/30/…– Martin BorthiryCommented Apr 25, 2014 at 6:25
-
Normally I would flag this as asking someone to do your work for you or a duplicate... But it is a good chance to highlight tools for this purpose and I couldn't find a canonical question to this effect.– Adrian Larson ♦Commented May 29, 2015 at 22:46
3 Answers
https://github.com/abhinavguptas/Salesforce-Lookup-Rollup-Summaries
The above link from Abhinav solves your problem
If you need for more than one object would recommend to try the excellent tool from Andrew
http://andyinthecloud.com/2013/07/07/new-tool-declarative-rollups-for-lookups/
And if you have requirement for only one object ,you can prefer jeff solution as well
http://blog.jeffdouglas.com/2009/07/30/roll-up-summary-fields-with-lookup-relationships-part-1/
-
3Yeah I would generally put Fawcett tops on the list.– Adrian Larson ♦Commented May 29, 2015 at 22:42
Here is an example :
- Account (Parent Object)
- Contact (Child Object).
- Contact_Recs__c (Roll up summary field/Custom Field).
- accountid (Lookup field).
Code sample:
trigger CountContactsnew on Contact (after insert, after delete, after undelete) {
List<id> accIdList = new List<id>();
if(Trigger.isInsert || Trigger.isUndelete){
For(Contact con1 : Trigger.new){
accIdList.add(con1.accountid);
}
}
if(Trigger.isDelete){
For(Contact con1 : Trigger.old){
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());
}
}
-
did you mean to use the in 'operator' instead of the 'is'? in as opposed to = Commented Jan 5, 2017 at 23:31
I would also recommend this 'no code' Flow solution which I have used in the past: https://salesforceweek.ly/2015/02/how-to-create-roll-up-summaries-using-flow.html