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
OwnerIdto assign it to the correct queue
This approach allows us to centralize configuration and avoid hardcoding queue names inside the Apex class.
🔍 Code Explanation
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.
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.
Next, we query the specific Case record based on the provided ID.
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-catchblocks for safer updatesMaking it part of a trigger or a Process Builder with an
@InvocableMethod
🧾 Final Code Snippet
🎥 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.
Want to go further with this?
Learn it step by step in a course, or talk it through on a 1:1 call.
