When managing support or service operations in Salesforce, you might have different queues set up for specific case types like SLA Queue, Escalation Queue, etc. But what if you want to combine all cases from two queues into a single view or report?
This blog post shows you how to write a simple Apex method that:
Accepts two queue names
Pulls all cases owned by those queues
Merges them into a single Set (to avoid duplicates)
Returns the result
This is super useful when you’re building custom dashboards, analytics tools, or just want to simplify queue-level case tracking.
🧠 What Are We Trying to Do?
Here’s our goal:
Input: Two queue names (as strings)
Output: A merged list of
Case
records owned by those two queues
We’ll use a Set<Case>
to store the results because Sets automatically remove duplicates which is helpful if some records appear in both lists (rare, but better safe than sorry).
🔍 Code Explanation
public with sharing class ApexUseCaseTwentyFive {
public static Set<Case> returnMergedList(String queueName1, String queueName2){
We define a class ApexUseCaseTwentyFive
and a static method that takes two queue names as parameters.
Set<Case> mergedList = new Set<Case>();
We initialize a Set to store all the cases we retrieve. Using a Set
ensures that if the same case appears in both queries, we don’t end up with duplicates.
List<Case> escalationRecords = [SELECT Id FROM Case WHERE Owner.Name = :queueName1];
List<Case> slaRecords = [SELECT Id FROM Case WHERE Owner.Name = :queueName2];
Next, we run two SOQL queries to get cases owned by each of the provided queues. We’re filtering by Owner.Name
, which is how queue ownership is represented on Case records.
mergedList.addAll(escalationRecords);
mergedList.addAll(slaRecords);
We add both lists to the set. If there are any overlapping cases (with the same ID), only one will be stored in the final result.
return mergedList;
}
}
Finally, we return the full merged set of cases.
✅ Why This Is Useful
This method is a practical tool for:
Building custom case views across multiple teams
Aggregating queue-based workloads into one screen
Performing batch processing for multiple queues
You can even enhance it by:
Adding filters (e.g., Status = ‘Open’)
Returning more fields like
Subject
,Priority
, etc.Converting the Set to a List if your use case prefers lists
🧾 Final Code Snippet
public with sharing class ApexUseCaseTwentyFive {
public static Set<Case> returnMergedList(String queueName1, String queueName2){
Set<Case> mergedList = new Set<Case>();
List<Case> escalationRecords = [SELECT Id FROM Case WHERE Owner.Name = :queueName1];
List<Case> slaRecords = [SELECT Id FROM Case WHERE Owner.Name = :queueName2];
mergedList.addAll(escalationRecords);
mergedList.addAll(slaRecords);
return mergedList;
}
}
🎥 Watch It in Action!
Prefer to watch how this works instead of reading through code? Check out this quick demo video where I walk through the entire logic using two queues in a Salesforce org.