Is it possible to have a Map (or other collection) of sObjects list, keyed by one lookup field of the same sObject.
Something like Maps of sObjects (below code doesn't works):
Map<Id, List<Custom__c>> itemsMap = new Map<Id, List<Custom__c>>([SELECT Lookup_To_Custom__r.Id, Name FROM Custom__c]);
Note: this gives the following error Invalid initial type LIST<Custom__c> for MAP<Id,LIST<Custom__c>>
Were the Id
in Map<Id, List<Custom__c>>
is a reference to the Lookup_To_Custom__r.Id
field.
Best Answer
The special map constructor only handles the case of creating a map where the key is the ID and the value is the SObject.
To do what I think you want to do requires a loop:
Note that using the foreign key field
Lookup_To_Custom__c
is a little more direct than going through the reference and taking the IDLookup_To_Custom__r.Id
.