1

I want to know if it is possible through a map or some form of multidimentional List type to contain more than 2 parameters. For example I have a nice SOQL here that returns a column for Contact, Quote, and CampaignMember (each row tranversed by ContactId)

Map<Contact, Quote, CampaignMember> masterMap = [
SELECT 
    Id, 
    (SELECT Id, Name FROM Quotes), 
    (SELECT Id FROM CampaignMembers) 
FROM Contact 
WHERE Id IN :contact_list
]

How do I store the result from a SOQL like this in a way I can use? My only other alternative I can foresee myself is to have two separate queries (see below) which I'm hoping is avoidable.

Map<Contact,Quote> quoteMap = [SELECT Id, (SELECT Id, Name FROM Quotes) FROM Contact WHERE Id IN :contact_list]

Map<Contact,CampaignMember> membersMap = [SELECT Id, (SELECT Id FROM CampaignMembers) FROM Contact WHERE Id IN :contact_list]
1
  • I know you can do something like this Map<Id,List<Id>>, not sure if it helps but you might find it useful. Jan 29 '15 at 20:58
2

Ok I think I found a solution that works, thanks to this post

Map<Id, List<Quote>> QuoteMap = new Map<Id,List<Quote>>();
Map<Id, List<CampaignMember>> CampaignMemberMap = new Map<Id,List<CampaignMember>>();

List<Contact> contacts = [SELECT Id, (SELECT Id, Name FROM Quotes), (SELECT Id FROM CampaignMembers) FROM Contact WHERE Id IN :contact_list];
for(Contact c : contacts){
    QuoteMap.put(c.Id, c.getSObjects('Quotes')); // put the Contact Quotes in a map by ContactId
    CampaignMemberMap.put(c.Id, c.CampaignMembers); // put the Contact CampaignMembers in a map by ContactId without using getSObjects method as mentioned in the comments
}
3
  • 1
    No need to cache the results! You can just take the first two lines of that for loop and merge them into the corresponding put call. e.g. QuoteMap.put(contact.Id, contact.getSObjects('Quotes'));
    – Adrian Larson
    Jan 29 '15 at 21:24
  • 1
    Technically don't even need the getSObjects part since you have them declared, i.e. can just do QuoteMap.put(c.Id, c.Quotes);
    – ScottW
    Jan 29 '15 at 22:14
  • When would I need getSObjects then because I tried to remove them from the SOQL but got an error that I was attempting a field that was not accessible from my result? Jan 29 '15 at 22:17

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.