In Salesforce, tracking support cases is a core part of maintaining strong client relationships. But not all cases are created equal—Partner Cases and Customer Cases often represent different types of interactions, priorities, and teams. To maintain clarity and quick reporting, it’s essential to separate and track these case types at the Account level.
In this blog, we’ll walk through a powerful Apex trigger that automatically updates the number of Partner Cases, Customer Cases, and Total Cases every time a new Case is created and linked to an Account. This enables real-time visibility into the nature of support activity, without requiring users or admins to manually count or update these fields.
This trigger runs in the after insert context and leverages Record Type differentiation to classify Cases by type.
🧠 Why This Trigger Is Important
As your Case volume grows, it becomes harder to keep track of how many are coming from partners versus customers—especially if you’re serving both audiences. Accurate categorization helps:
-
Identify which Accounts are partner-heavy vs. customer-heavy
-
Spot support trends by audience type
-
Allocate resources across service teams efficiently
-
Improve reporting for SLAs, satisfaction metrics, or support goals
-
Maintain data consistency for executive dashboards
Without automation:
-
Field values can become outdated or incorrect
-
Reports may reflect inconsistent data
-
Users may be forced to manually recount and update case totals
With this trigger:
-
All new Cases are evaluated immediately after creation
-
Each Account’s Customer Case, Partner Case, and Total Case fields are updated automatically
-
Your CRM stays accurate and aligned with real-world interactions
🔍 What This Blog Covers
-
How to use an Apex trigger to classify Cases by Record Type
-
How to calculate summary counts and update parent records (Accounts)
-
The importance of clean handler logic for scalability
-
How to differentiate logic using
DeveloperName
andName
values for Record Types -
Why real-time case categorization is important for support teams
This logic makes your CRM smarter—and your support teams more informed—without adding extra work for end users.
🎯 Real-World Use Cases for This Trigger
-
Channel support managers tracking partner-related queries separately from customers
-
Customer service departments measuring ticket volumes by case origin
-
Sales teams identifying which accounts are support-heavy
-
CX teams identifying customers who may need attention or outreach
-
Organizations using partner portals that want visibility into partner interactions
This trigger fits perfectly in any org where partner and customer support need to be tracked and analyzed separately but tied to the same Account.
👨💻 Developer & Admin Tips
Let’s break down how the trigger works:
-
It activates after a Case is inserted
-
It collects all unique Account IDs from the new Cases
-
It retrieves all existing Cases related to those Accounts
-
It separates and counts Cases by Record Type:
-
Partner Cases using the
DeveloperName
orName
field -
Customer Cases using a similar check
-
-
It then updates each Account’s:
-
Partner_Case__c
-
Customer_Case__c
-
Total_Case__c
-
The logic is wrapped in a clean handler method, which:
-
Keeps code modular and easy to test
-
Prepares the solution for future enhancements
-
Ensures bulk-safe processing of multiple records
You can expand this setup by:
-
Including other Case types (like Internal or Escalation)
-
Using the logic in update or delete contexts if case types change
-
Triggering notifications when case volume hits a threshold
Make sure to test:
-
Single Case insertions
-
Batch inserts (e.g., via Data Loader)
-
Accounts with existing Cases to validate correct count updates
-
Edge cases like invalid Record Types or missing Account IDs
🎥 Step-by-Step Visual Demo – YouTube Playlist
Want to implement this with confidence? Head over to the Salesforce Makes Sense YouTube playlist for a guided walkthrough. The video includes:
-
How to write and test this Apex logic
-
Real-time validation of field updates on Accounts
-
Tips for managing Record Types efficiently in code
-
Ideas for customizing the logic further based on business needs
The playlist helps translate technical logic into actionable CRM impact.
Solution:
trigger CaseTrigger on Case (after insert) {
if(Trigger.isInsert){
if(Trigger.isAfter){
CaseTriggerHandler.countCases(Trigger.new);
}
}
}
public class CaseTriggerHandler{ public static void
countCases(List cList){
List<Account> accList= new List<Account>();
Set<Id> idSet= new Set<Id>();
Id partnerCaseRecordTypeId = [Select Id From RecordType Where
DeveloperName = ‘Partner_Case’].Id;//1.way to do with
DeveloperName which is API name
Id customerCaseRecordTypeId=[Select Id From RecordType Where
Name = ‘Customer Case’].Id;//2.way to do with Name for(Case c:cList){
if(c.AccountId!=null){ idSet.add(c.AccountId);
}
}
for(Account acc:[SELECT
Id,Total_Case__c,Customer_Case__c,Partner_Case__c,(SELE
CT Id,RecordTypeId FROM Cases) FROM Account WHERE Id
IN:idSet]){
decimal countPartner=0; decimal countCustomer=0;
for(Case c:acc.Cases){
if(c.RecordTypeId==partnerCaseRecordTypeId){
countPartner++;
}else if(c.RecordTypeId==customerCaseRecordTypeId){ countCustomer++;
}
}
acc.Customer_Case__c=countCustomer;
acc.Partner_Case__c=countPartner;
acc.Total_Case__c=acc.Customer_Case__c+acc.Partner_Case
__c;
accList.add(acc);
}
if(!accList.isEmpty()){
update accList;
}
}
}