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

trigger to get the total number of close cases per contact

Hi all,



I cannot create a Roll-Up Summaryfield on Contats to see the total of close cases in the contact detail page.


Any sample code for this?


Tank you


Please find the below link for sample code regarding creating the custom roll-up summary

/* Provide summary of Number of Cases on Contact record */

trigger CasestSumTrigger on Case (after delete, after insert, after undelete,
after update) {

    Case[] cas;
    if (Trigger.isDelete)
        cas = Trigger.old;
        cas =;

    // get list of Contacts 
    Set<ID> contIds= new Set<ID>();
    for (Case cs : cas) {
    Map<ID, Case> CasesForContact = new Map<ID, Case>([select Id
                                                            from Case
                                                            where ContactId in :contIds]);

    Map<ID, Contact> contToUpdate= new Map<ID, Contact>([select Id
                                                                  from Contact
                                                                  where Id in :contIds]);
    for (Contact cont: contToUpdate.values()) {
        Set<ID> csIds = new Set<ID>();
        for (Case cs : CasesForContact.values()) {
            if (cs.ContactId == cont.Id)
        if (cont.Number_of_Cases__c != csIds .size())
            cont.Number_of_Cases__c =  csIds .size();

    update contToUpdate.values();

 I managed to create this but still got an error:

Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<Case> at line 32 column 27





Error is because of this csIds.add(cas.Id);    // Cas is an array