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
mlindsaymlindsay 

How do I create a trigger to count the number of open cases on an account

All,

 

I need to create trigger to count the number of open cases on an account.  I need to display this information on the account page.

 

Thanks for the help,

 

Michael

hitesh90hitesh90

Hi,

 

here is the example to count number of cases in Account through trigger

 

trigger tskUpdatetrigger on Case (after insert, after update,after delete) {
    List<Case> lstCase = [select id, AccountId from Case where id in: trigger.newmap.keyset()];
    set<Id> sAccId = new set<Id>();
    for(Case cs: lstCase){
        if(cs.AccountId != null){
            sAccId.add(cs.AccountId);
        }
    }
    if(sAccId != null && sAccId.size() > 0){
        List<Account> lstAccount = [select id, Count_Cases__c, (select id from Cases) from Account where id in: sAccId];
        if(lstAccount.size() > 0){
            for(Account acc: lstAccount){
                acc.Count_Cases__c = acc.Cases.size();
            }
            
            update lstAccount;
        }
    }
}

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

 

mlindsaymlindsay
Hitesh,



The trigger works great to count the total number of cases on an account
but I need to only count the open cases. Could you please include that in
the trigger?



Thanks,



Michael



* *

*Michael Lindsay*
Sales Enablement Manager
Office: 408-962-3914 | Mobile: 408-718-7524

*www.picarro.com*

*World's Leading Instruments* *for Carbon and Water Cycle Measurements*
hitesh90hitesh90

Hi Michael,

 


below is the updated trigger.

 

trigger tskUpdatetrigger on Case (after insert, after update,after delete) {
    List<Case> lstCase = [select id, AccountId from Case where id in: trigger.newmap.keyset()];
    set<Id> sAccId = new set<Id>();
    for(Case cs: lstCase){
        if(cs.AccountId != null){
            sAccId.add(cs.AccountId);
        }
    }
    if(sAccId != null && sAccId.size() > 0){
        List<Account> lstAccount = [select id, Count_Cases__c, (select id from Cases where status = 'Open') from Account where id in: sAccId];
        if(lstAccount.size() > 0){
            for(Account acc: lstAccount){
                acc.Count_Cases__c = acc.Cases.size();
            }
            
            update lstAccount;
        }
    }
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

Coral RacingCoral Racing
Hi Hitmesh

I am trying to do something similar for an application called RemedyForce which is a Service Desk application.  I am wanting to find the number of Incidents (BMCServiceDesk__Incident__c) created against a User (BMCServiceDesk__FKClient__c) and store it in a cutom field (IncidentCount__c)

How would adapt the trigger above to do this?

Thanks

Sonya
Coral RacingCoral Racing
ried this...but as you can see I am getting a bit lost.  BMCServiceDesk__FKClient__c is a look up field to the User object

trigger tskUpdatetrigger on BMCServiceDesk__Incident__c(after insert, after update,after delete) {
02        List<BMCServiceDesk__Incident__c> lstIncident = [select id, BMCServiceDesk__FKClient__c from Incident where id in: trigger.newmap.keyset()];
03        set<Id> sBMCServiceDesk__FKClient__c = new set<Id>();
04        for(Incident in: lstIncident){
05            if(in.BMCServiceDesk__FKClient__c != null){
06                sBMCServiceDesk__FKClient__c.add(in.BMCServiceDesk__FKClient__c);
07            }
08        }
09        if(sBMCServiceDesk__FKClient__c != null && sBMCServiceDesk__FKClient__c.size() > 0){
10            List<BMCServiceDesk__FKClient__c> lstClient = [select id, IncidentCount__c, (select id from Cases where status = 'Open') from Account where id in: sAccId];
11            if(lstClient.size() > 0){
12                for(Account acc: lstClient){
13                    acc.IncidentCount__c= acc.Cases.size();
14                }
15                 
16                update lstIncident;
17            }
18        }
19    }
 
MrBrianMrBrian
@hitesh90

Thank you so much for this trigger, however I am getting a 'de-reffrencing a null object' error in line 2 column 1 when deleting the case only. The only change I made is status ='Open' changed to status !='Closed'. I am a novice when it comes to triggers - can you please help?

Much appreciated,
Brian