Here is the case I am working with now:

There is a Custom_Object__c linked to Case object via lookup field (Custom_Object__c is a parent). After a new case is created, Case_Count__c field should be updated on Custom_Object__c with the number of cases tied to that object. Here is the sample code I wrote that adds +1 on Case_Count__c field every time a new case is inserted(works fine):

trigger CaseCount on Case (after insert) {
List<Case> lstCase = [select id, Custom_object_lookup__c, Type from Case where id in: trigger.newmap.keyset()];
set<Id> custObjId = new set<Id>(); 
for(Case cs: lstCase){
    if(cs.Custom_object_lookup__c != null) {


    if(custObjId != null && custObjId.size() > 0) { 

    List<Custom_object__c> lstCustomObject= [select id, Case_Count__c from Custom_object__c where id in: custObjId ]; 

    if(lstCustomObject.size() > 0){
        for(Custom_object__c custObj: lstCustomObject){
            System.debug('Case count before update: ' + custObj.Case_Count__c);
            custObj.Case_Count__c += 1;
            System.debug('Case count after update: ' + custObj.Case_Count__c);


      update lstCustomObject;



Question: What would be correct way/approach to query all the cases related to Custom_object__c and update the Case_count__c field accordingly?

  • The lookup goes from Case to the Custom Object right? That means the Case isn't the parent but the Custom Object, might want to change that in your question for clarity. – Folkert May 26 '16 at 13:28
  • I feel you should consider update scenario as well. What if someone change the lookup value . Count will be affected. – Mukesh Verma May 26 '16 at 13:39
  • @MukeshVerma sure, update and delete scenarios will be implemented as well but the question is about finding the right approach of querying the related records – Juris Upenieks May 26 '16 at 13:53
  • 1
    @Juris Upenieks: There are two ways for counting: (1) On every DML recount related records and reset count field. This will require SOQL everytime. (2) Set default field value of count to zero and with every new record creation keep increasing count. I feel approach B is better comparatively. You can consider that as well. – Mukesh Verma May 26 '16 at 14:08
  • @MukeshVerma thanks, actually I was considering 2nd option and actually was working towards it but thought that 1st option you mentioned would fit better therefore the question – Juris Upenieks May 26 '16 at 14:58

Rather than coding your own solution, I'd recommend using DLRS to do the work for you. It is a free tool and very powerful. It will let you rollup counts as part of inserts, and also run rollups on a schedule or adhoc. It will also solve some problems that you haven't addressed in your code such as deletes of cases or changes to the custom object lookup field.

If you want to write your own code, you can use an aggregate query to get a count of cases this way:

SELECT Custom_object_lookup__c, Count(Id) 
FROM Case 
GROUP BY Custom_object_lookup__c
  • Thanks for the advice but the application has free and enterprise editions and real-time roll-up execution is available only in the enterprise edition so it is not suitable in this case – Juris Upenieks May 26 '16 at 13:52
  • You're thinking of a different tool called Rollup Helper. DLRS is completely free. – Daniel Hoechst May 26 '16 at 13:53
  • sincerely apologize, I was in hurry and Rollup Helper was installed in my dev.org so that caused the confusion. Will try DLRS, thanks! – Juris Upenieks May 26 '16 at 13:54
  • Fantastic app, worked like a charm, highly recommend to anyone, @DanielHoechst, thanks for recommendation! – Juris Upenieks May 27 '16 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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