I have a map for an object for which I want to use a name field on the object as a key and value as map huishoudMap = new map

Is there something similar to map ?

How do I achieve this using map:

map<name,Huishouden__c> huishoudMap = new map<name,Huishouden__c>([
    SELECT Id,
        (SELECT id, Huishouden__c FROM Relatie_Contacten__r)
    FROM Huishouden__c]);

for (Relatie__c rc: rcList) {
    string relKeyId = rc.Contactpersoon__r.MailingPostalCode + rc.Contactpersoon__r.MailingStreet + ' ' + rc.Account__r.Relatienummer__c;
    system.debug('rc.Huishouden__c '+rc.Huishouden__c);
    if (huishoudMap.containsKey(relKeyId)) {
        rc.Huishouden__c =  huishoudMap.get(relKeyId).id;
    } else {

    update rcList;   

In Apex you can construct only Map<Id, sObject> from SOQL query result. If you want to get Name field as key, you would use Map<String, sObject>


Map<String ,Huishouden__c> huishoudMap = new Map<String, Huishouden__c>();

for(Huishouden__c hs : [SELECT Id, Name, (SELECT id, Huishouden__c FROM Relatie_Contacten__r) FROM Huishouden__c]) {
    huishoudMap.put(hs.Name, hs);
| improve this answer | |
  • I have to disagree with you on your first sentence. You can use any type to build the map. Quoting the doc: "Map keys and values can be of any data type, including sObject types, such as Account." developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – POZ May 23 '18 at 13:03
  • @POZ, please refer to paragraph Auto-Populating Map Entries from a SOQL Query from the same document. You can declare the Map as new Map<String, sObject>([SOQL]), but the key will be taken from Id field anyway. – Vladimir Egikyan May 23 '18 at 13:10
  • @POZ I think there's some confusion in terminology here. When I hear "build a map", I think "insert data into the map". I'd call what you're talking about "declaring" a map. While it's true that a Map can use basically any type as the key, if you're using the Map constructor that takes a List<SObject>, like Map<Id, SObject> myMap = new Map<Id, SObject>([<query here>]); (as OP is trying), that will only work if the map key is an Id. There is a way around that using aggregate functions and field aliasing, but that approach has some drawbacks and I don't think it's worth the trouble. – Derek F May 23 '18 at 13:12
  • Yes, true. I should have written 'construct' instead of 'build'. Sorry for the confusion. – POZ May 23 '18 at 13:19

The problem that you are facing is that Name is not a type so you cannot use it in your Map declaration (line 1).

You should use a String instead:

Map<String,Huishouden__c> huishoudMap = new Map<String,Huishouden__c>();
for (Huishouden__c record: [SELECT Id, Name, (SELECT id, Huishouden__c FROM Relatie_Contacten__r) FROM Huishouden__c]) {
    huishoudMap.put(record.Name, record);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.