+ Start a Discussion
ReeaDeveloperReeaDeveloper 

Create a Map<String, CustomObject> with SOQL query

Hello. I want to know how can i create a Map<String, CustomObject> where the key will not be the Id of the custom field but the Name field and without using a list and going through each record and puting in the map a pair.

If I use Map<String, CustomObject__c> myMap = new Map<String, CustomObject__c>([Select z.Name, z.Id From CustomObject__c z]); i have a map where the key is my CustomObject.Id and I want it to be CustomObject.Name.

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Hi ReeaDeveloper,

For that you have to iterate through the list and keep putting the key and objects.

 

Map<String, CustomObject__c> myMap = new Map<String, CustomObject__c>(); 
for(CustomObject__c objCS : [Select z.Name, z.Id From CustomObject__c z])
        myMap.put(objCS.Name, objCS);

 Make sure name is mandatory or put a condition that string name must not be blank before filling the object into map.

Hope it helps.

All Answers

Rahul SharmaRahul Sharma

Hi ReeaDeveloper,

For that you have to iterate through the list and keep putting the key and objects.

 

Map<String, CustomObject__c> myMap = new Map<String, CustomObject__c>(); 
for(CustomObject__c objCS : [Select z.Name, z.Id From CustomObject__c z])
        myMap.put(objCS.Name, objCS);

 Make sure name is mandatory or put a condition that string name must not be blank before filling the object into map.

Hope it helps.

This was selected as the best answer
Kabb12Kabb12
Map<String, CustomObject__c> myMap = new Map<String, CustomObject__c>([Select z.Name, z.Id From CustomObject__c z]);
Akshay Jain 82Akshay Jain 82
Just want to ask whether it is Ok, if we do not use the proxy variable z, i.e if we just write [select Name, Id from CustomObject__c]
Afroz Ahmed 9Afroz Ahmed 9
// Populate map from SOQL query
Map<ID, Account> m = new Map<ID, Account>([SELECT Id, Name FROM Account LIMIT 10]);
// After populating the map, iterate through the map entries
for (ID idKey : m.keyset()) {
    Account a = m.get(idKey);
    System.debug(a);
}
akhil chintalaakhil chintala
hello every one
i have one task will anyone please solve this(im a fresher)plz dont mind
Whenever new Lead is created we have to check for users who have the less number of leads, we have to assign that lead to that particular user.
U1  L1 L4
U2  L2 L5
U3  L3
Now when new Lead is created it will check users with leads LIST,and assign created new lead to that User who has less number of Leads.

Create a custom field(LeadCount) in User Object
Query to get user and LeadCount field  and put them in one map
Sort that map
Whichever user has lessnumber of leadscount try to add the lead for that user.
farukh sk hdfarukh sk hd
These posts will cover about list,set,map usage and methods available and how to use map.

https://www.sfdc-lightning.com/2018/09/collection-in-salesforce.html
 
Hari KundrapuHari Kundrapu
@kaab12,
Map<String, CustomObject__c> myMap = new Map<String, CustomObject__c>([Select z.Name, z.Id From CustomObject__c z]);

For this, the key value is still Id but in the format of String instead of Name.
Adam Cadman 4Adam Cadman 4

The answer that should have been marked as 'Correct' was the one by @Kabb12.

The one that was flagged as correct by 'Admin' did what the OP explicitly asked not to do - loop through the list of records and add them to the Map one at a time.

You don't need to use the 'z' alias though.

JaimeBidJaimeBid

I must be missing something.

Kabb12 seems the best answer:
Map<String, CustomObject__c> myMap = new Map<String, CustomObject__c>([Select z.Name, z.Id From CustomObject__c z]);

But the code he provides, is the same as the included in the question:
If I use

Map<String, CustomObject__c> myMap = new Map<String, CustomObject__c>([Select z.Name, z.Id From CustomObject__c z]);

i have a map where the key is my CustomObject.Id and I want it to be CustomObject.Name.

Adam CadamAdam Cadam

@Kabb12 's answer is incorrect - this still produces a Map with the Id as the key (albeit with the Id as a String type not an Id).

The order of the fields in the SOQL statement does not make a difference - when Apex casts the list to a Map it uses trhe Id as the Key.

In fact, you don't even need to include the Id as a field in the SOQL - I tried this with one of my Custom types and it works just fine, with the Id still be used as the Key:

Map<String, CustomType__c> myMap = 
    new Map<String, CustomType__c>([Select Name, Description__c From CustomType__c]);
system.debug(myMap);

So, it is NOT possible to do what the OP asked for - create a Map with the Name as the Key, without using a loop to iterate over the list and build the map one record at a time.