0

The below is a valid way to create a Map.

Map<Id, Opportunity> mapOpps = new Map <Id, Opportunity>();

I know that I can use String in place of ID. BUT could someone please explain to me why can't I use Name, as shown?

Map<Name, Opportunity> mapOpps = new Map <Name, Opportunity>();
1
  • 2
    Pro tip: naming conventions for maps: maps of Opportunities by Id should not be named mapContacts. I'd name it opportunitiesById - the ...by... is the cue it is a map.
    – cropredy
    Commented Mar 23, 2019 at 18:32

2 Answers 2

4

The Type for this field is String. There is no Type called Name. Note also that the Id Map is the only built in constructor. For any other map, you will need to construct it empty then loop to populate. One more note, this field is not unique. If you have multiple records with the same name, it is arbitrary which one ends up in the map.

Map<String, MyObject> nameToRecord = new Map<String, MyObject>();
for (MyObject record : someCollection)
    nameToRecord.put(record.Name, record);
3
  • To clarify, when you use "Id" in the type declaration of a map, it is the type NOT the field. Salesforce provides a special constructor to convert a List<SObject> into a Map<Id, SObject>.
    – Phil W
    Commented Mar 23, 2019 at 16:34
  • NB: It is possible to use a static initializer for a Map to avoid looped put if you have a defined number of entries to add. E.g. new Map<String, String> { 'key' => 'value' }.
    – Phil W
    Commented Mar 23, 2019 at 16:36
  • There is a Name type, though. It's used for polymorphic "people" fields, like Owner. You're still correct in the solution, but you might want to correct the Name part.
    – sfdcfox
    Commented Mar 23, 2019 at 18:25
1

why can't I use Name, as shown? Map<Name, Opportunity>

Because it’s not a valid data type for the key. Name is a field either text or auto number for any SObject and that only the following are valid data types for a Map’s keys and values:

Map keys and values can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.

4
  • Again, to clarify, Name is NOT a type, it is a field name.
    – Phil W
    Commented Mar 23, 2019 at 16:38
  • Thank you all for advice! I understand now. Key has to be a data type, like those here: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – Seyitbek
    Commented Mar 23, 2019 at 16:39
  • 1
    Name is a valid data type. It represents polymorphic "people" fields, such as Owner. Granted, it's not what the user needs, but you should be aware of this feature.
    – sfdcfox
    Commented Mar 23, 2019 at 18:26
  • I wouldn’t really think it to be a data type though. It could still be a type polymorphic field but does not really categorize as data type in context of apex. The data type of Name itself will be either text or number.
    – Jayant Das
    Commented Mar 24, 2019 at 0:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .