Apex Trigger Whenever Case is: Inserted/Updated/Deleted/Undeleted We need to count Cases based on Priority: High, Medium, Low And update counts on Account
Step 1 Create Fields on Account
Create 3 Number fields:
| Field Label | API Name |
|---|---|
| High Priority Cases | High_Priority_Cases__c |
| Medium Priority Cases | Medium_Priority_Cases__c |
| Low Priority Cases | Low_Priority_Cases__c |
Data Type โ Number (0 decimal)
Logic Pattern (Same Clean Structure)
- Collect AccountIds
- Run ONE Aggregate SOQL
- Store result in separate Maps
- Update Accounts
Step 2 Case Trigger (After Update): CasePriorityCountTrigger.apxt
TRIGGER CasePriorityCountTrigger ON CASE
(after INSERT, after UPDATE, after DELETE, after undelete) {
Set<Id> accountIds = NEW Set<Id>();
// Collect AccountIdsIF(TRIGGER.isInsert || TRIGGER.isUpdate || TRIGGER.isUndelete){
FOR(CASE cs : TRIGGER.new){
IF(cs.AccountId != NULL){
accountIds.add(cs.AccountId);
}}}IF(TRIGGER.isDelete){
FOR(CASE cs : TRIGGER.old){
IF(cs.AccountId != NULL){
accountIds.add(cs.AccountId);
}}}IF(accountIds.isEmpty()){
RETURN;}// Separate maps FOR each priority
Map<Id, Integer> highMap = NEW Map<Id, Integer>();
Map<Id, Integer> mediumMap = NEW Map<Id, Integer>();
Map<Id, Integer> lowMap = NEW Map<Id, Integer>();
// Aggregate QueryList<AggregateResult> results = [
SELECT AccountId, Priority, COUNT(Id) totalCount
FROM CASE
WHERE AccountId IN :accountIds
AND Priority IN ('High','Medium','Low')
GROUP BY AccountId, Priority
];// Store counts IN respective maps
FOR(AggregateResult ar : results){
Id accId = (Id) ar.get('AccountId');
String priorityValue = (String) ar.get('Priority');
INTEGER countValue = (INTEGER) ar.get('totalCount');
IF(priorityValue == 'High'){
highMap.put(accId, countValue);
}ELSE IF(priorityValue == 'Medium'){
mediumMap.put(accId, countValue);
}ELSE IF(priorityValue == 'Low'){
lowMap.put(accId, countValue);
}}List<Account> accountList = NEW List<Account>();
// UPDATE Accounts
FOR(Id accId : accountIds){
Account acc = NEW Account();
acc.Id = accId;
acc.High_Priority_Cases__c =
highMap.containsKey(accId) ? highMap.get(accId) : 0;
acc.Medium_Priority_Cases__c =
mediumMap.containsKey(accId) ? mediumMap.get(accId) : 0;
acc.Low_Priority_Cases__c =
lowMap.containsKey(accId) ? lowMap.get(accId) : 0;
accountList.add(acc);
}IF(!accountList.isEmpty()){
UPDATE accountList;}}
Why This Is Best Practice?
- Single Aggregate SOQL
- Uses GROUP BY AccountId, Priority
- No SOQL inside loop
- No DML inside loop
- Fully bulkified
- Easy to explain
- Production-ready pattern
What are the major limitations of apex triggers?
Your Apex triggers (combined) must not exceed 200 SOQL queries per batch. If they do, your Clean job for that object fails. In addition, if your triggers call future methods, they're subject to a limit of 10 future calls per batch.
What are common Apex trigger mistakes?
Summary. Salesforce Triggers are essential for automating business logic, but common mistakes like not bulkifying code, writing logic directly in triggers, missing recursion control, and hardcoding values can lead to performance issues and deployment failures.
What are the two types of triggers in Salesforce?
Before Triggers: These triggers are used to update or validate triggering record values before they're saved to the database. After Triggers: These are used to access fields values that are already set by the system like recordId, lastModifiedDate field for performing after save actions like sending mail.
Related Topics | You May Also Like
|
๐ Get Free Course โ
๐ Salesforce Administrators ๐ Salesforce Lightning Flow Builder ๐ Salesforce Record Trigger Flow Builder |
๐ Get Free Course โ |


