Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there anyway to update a rollup summary field with lookup relation ship for a custom object. How can we achieve this using triggers ?

share|improve this question
Sorry, I've deleted my answer, because I misunderstood the question. Here you have a good article to follow:… – Martin Borthiry Apr 25 '14 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 May 29 '15 at 22:46
up vote 8 down vote accepted

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

And if you have requirement for only one object ,you can prefer jeff solution as well

share|improve this answer
Yeah I would generally put Fawcett tops on the list. – Adrian Larson May 29 '15 at 22:42

Here is an example :

  1. Account (Parent Object)
  2. Contact (Child Object).
  3. Contact_Recs__c (Roll up summary field/Custom Field).
  4. 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 :{
        For(Contact con1 : Trigger.old){
    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());
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.