function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Marc BehrMarc Behr 

Creating a Map with the object name as the key

Hi,

I am trying to write a Apex query to store the Id's of the queues that I have created. I then want to be able to quickly look up the Id of a specific queue by name.

What  I want is a map where the key is the name of the Queue and the Id is the value, so I wrote the following:

Map<String,Group> Queues = new Map<String, Group>([Select Name,Id from Group where Type = 'Queue']);

 

When I do this, instead of the Name being the key, the Id is.  
(I am using "system.debug(Queues.keyset());" to see the keys

How do I create a Map with the Queue names as the key? The only way that I have been able to come up with so far is something like this:

 

 

Group[] X;
X = [Select Id, Name from Group where Type = 'Queue'];

Map<String,String> Q1 = new Map<String,String>();

for(Integer i = 0; i < X.size(); i++) {
Q1.put(X[i].Name, X[i].Id);
}

But this seems like overkill to run this each time a new email message comes in (the code will be part of an email handler that assigns new cases to the correct queue based on who the message is from.

 

 

Thanks!

Marc

Best Answer chosen by Admin (Salesforce Developers) 
EMHDevEMHDev

You've answered your own question.  The use of SOQL in defining the map is a shorthand way to set up a map of Ids to Sobjects.  The Apex Reference Guide says:

"Maps from an ID or String data type to an sObject can be initialized from a list of sObjects. The IDs of the objects (which must be non-null and distinct) are used as the keys."

Otherwise you need to put each entry into the map.

Perhaps you could use Custom List Settings for your purpose - populate it when you need to (presumably your queues are not that dynamic that they are being created and removed daily) and refer to it from your email handling code.  The nice thing about using that is that it doesn't count against governor limits (or at least against query limits), which may be an issue in handling emails.

All Answers

EMHDevEMHDev

You've answered your own question.  The use of SOQL in defining the map is a shorthand way to set up a map of Ids to Sobjects.  The Apex Reference Guide says:

"Maps from an ID or String data type to an sObject can be initialized from a list of sObjects. The IDs of the objects (which must be non-null and distinct) are used as the keys."

Otherwise you need to put each entry into the map.

Perhaps you could use Custom List Settings for your purpose - populate it when you need to (presumably your queues are not that dynamic that they are being created and removed daily) and refer to it from your email handling code.  The nice thing about using that is that it doesn't count against governor limits (or at least against query limits), which may be an issue in handling emails.

This was selected as the best answer
ahab1372ahab1372

I agree, you answered your own question. And no, it is not overkill, we all do it the same way.

 

Only thing you might want to change is your for loop. It is better to iterate through a SOQL query directly. That has several advantages when it comes to large numbers of rows returned. No need for "integer i ..." unless you need a counter in the loop.

See here for more:

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/langCon_apex_loops_for_SOQL.htm

 

 

Map<String,String> Q1 = new Map<String,String>();

for (Group theGroup:[Select Id, Name from Group where Type = 'Queue'])
{
   Q1.put(theGroup.Name,theGroup.Id);
}