You need to sign in to do that
Don't have an account?

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.
Hi ReeaDeveloper,
For that you have to iterate through the list and keep putting the key and objects.
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
Hi ReeaDeveloper,
For that you have to iterate through the list and keep putting the key and objects.
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.
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);
}
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.
https://www.sfdc-lightning.com/2018/09/collection-in-salesforce.html
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.
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.
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.
@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:
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.