In Salesforce, Cases come in through many different channels like Phone, Email, Web, or even Social Media. Often, you want to analyze or report on cases based on their origin so you know which channels are driving the most support requests.
In this blog post, we’ll create an Apex method that:
Queries all Case records (up to a limit)
Groups these cases by their
Origin
fieldReturns a map where the key is the origin (like ‘Phone’) and the value is a list of cases from that origin
This approach makes it easier to build custom dashboards, send reports, or apply specific logic to cases based on how they came in.
🧠 What Are We Trying to Do?
Suppose you want a quick summary of cases grouped by origin. For example:
How many cases came through Email?
Which cases were created via the Web channel?
Our method will:
Pull up to 50,000 cases (you can adjust the limit if needed)
Loop through each case
Organize them into groups by origin using a Map
The Map will look like this:
Origin | List of Cases |
---|---|
Phone | [Case1, Case2, Case3] |
[Case4, Case5] | |
Web | [Case6, Case7, Case8, Case9] |
🔍 Code Explanation
public with sharing class ApexUseCaseTwentyEight {
public static Map<String, List<Case>> generateCasesByOrigin(){
We start by defining a class ApexUseCaseTwentyEight
and a static method generateCasesByOrigin
which will return a Map where keys are Strings (case origin) and values are Lists of Case records.
Map<String, List<Case>> casesByOriginMap = new Map<String, List<Case>>();
We initialize an empty Map to store our results.
List<Case> caseRecords = [SELECT Id, CaseNumber, Origin FROM Case LIMIT 50000];
We query the Case object, pulling important fields including Origin
. The limit of 50,000 ensures the method can handle bulk data but can be adjusted for your org.
for(Case caseRec: caseRecords){
if(casesByOriginMap.containsKey(caseRec.Origin)){
casesByOriginMap.get(caseRec.Origin).add(caseRec);
}
else{
List<Case> casesByOriginList = new List<Case>();
casesByOriginList.add(caseRec);
casesByOriginMap.put(caseRec.Origin, casesByOriginList);
}
}
For each case, we check if the origin already exists as a key in the map:
If yes, we add the case to the existing list
If no, we create a new list, add the case, and then put it into the map
return casesByOriginMap;
}
}
Finally, we return the fully populated map.
✅ Why This Is Useful
This pattern helps when you want to:
Create custom reports or dashboards grouped by case origin
Apply different business logic based on the channel
Process cases in batches by origin (e.g., route web cases to a special queue)
You can extend this method by:
Filtering cases by date or status
Returning a count instead of full case lists
Adding other grouping keys like
Priority
orStatus
🧾 Final Code Snippet
public with sharing class ApexUseCaseTwentyEight {
public static Map<String, List<Case>> generateCasesByOrigin(){
Map<String, List<Case>> casesByOriginMap = new Map<String, List<Case>>();
List<Case> caseRecords = [SELECT Id, CaseNumber, Origin FROM Case LIMIT 50000];
for(Case caseRec: caseRecords){
if(casesByOriginMap.containsKey(caseRec.Origin)){
casesByOriginMap.get(caseRec.Origin).add(caseRec);
}
else{
List<Case> casesByOriginList = new List<Case>();
casesByOriginList.add(caseRec);
casesByOriginMap.put(caseRec.Origin, casesByOriginList);
}
}
return casesByOriginMap;
}
}
🎥 Watch It in Action!
If you prefer videos, watch this demo where I show the method in the Developer Console and explain how to leverage the grouped cases.