Imagine this: Your Salesforce org uses queues to manage support cases but the queue names could change depending on the environment (like Sandbox, Production, etc.). Instead of hardcoding the queue name in your Apex class, wouldn’t it be better to store it in a Custom Label and make your code dynamic?
That’s what we’ll cover in this blog post how to fetch a queue name from a custom label, get the matching queue record, and assign a newly created case to that queue. All in just a few clean lines of Apex!
Let’s dive in.
🧠 What Are We Trying to Do?
Here’s what we want this method to accomplish:
Accept the ID of a Case record
Read a Custom Label (
My_Primary_Queue
) that holds the queue’s Developer NameQuery the Group object to find a queue with that Developer Name
Update the Case’s
OwnerId
to assign it to the correct queue
This approach allows us to centralize configuration and avoid hardcoding queue names inside the Apex class.
🔍 Code Explanation
public with sharing class ApexUseCaseTwentySix {
public static void assignCaseToQueue(Id caseRecId){
We begin by defining the class ApexUseCaseTwentySix
and a static method that takes a single parameter: the ID of the Case we want to assign.
List<Group> queueInfo = [SELECT Id
FROM Group
WHERE Type = 'Queue'
AND DeveloperName = :System.Label.My_Primary_Queue
LIMIT 1];
We query the Group object to find a queue whose DeveloperName
matches the Custom Label My_Primary_Queue
. This label should be defined in Setup → Custom Labels.
List<Case> caseToAssign = [SELECT Id, OwnerId
FROM Case
WHERE Id = :caseRecId
LIMIT 1];
Next, we query the specific Case
record based on the provided ID.
if(caseToAssign.size() == 1 && queueInfo.size() == 1){
caseToAssign[0].OwnerId = queueInfo[0].Id;
update caseToAssign[0];
}
If both the Case and the Queue are found, we update the OwnerId
field on the Case to assign it to the queue.
And that’s it clean, dynamic, and fully configurable!
✅ Why This Is Useful
Using Custom Labels for queue names:
Makes your code portable across orgs (sandbox, production, etc.)
Helps you avoid hardcoded values
Keeps queue settings easily configurable for admins
You can enhance this logic by:
Adding error logging if the queue isn’t found
Using
try-catch
blocks for safer updatesMaking it part of a trigger or a Process Builder with an
@InvocableMethod
🧾 Final Code Snippet
public with sharing class ApexUseCaseTwentySix {
public static void assignCaseToQueue(Id caseRecId){
//Query this case, get the ownerid field and update it with the queue which is in the custom label
List<Group> queueInfo = [SELECT Id FROM Group WHERE Type = 'Queue' AND DeveloperName = :System.Label.My_Primary_Queue LIMIT 1];
List<Case> caseToAssign = [SELECT Id, OwnerId FROM Case WHERE Id = :caseRecId LIMIT 1];
if(caseToAssign.size() == 1 && queueInfo.size() == 1){
caseToAssign[0].OwnerId = queueInfo[0].Id;
update caseToAssign[0];
}
}
}
🎥 Watch It in Action!
Want to see it working inside Salesforce? Watch this short video where I create a custom label, reference it in Apex, and automatically assign a case to a queue.