Is it possible to output an aggregate query like this:

Map<Id, List<Contact>> accountContactsMap = [SELECT aggregate query ...];

Where the Map key is the AccountId and the Map value is a List of related Contact's for that Account?

If so, how should the query be constructed?

If not, what is the best approach to achieve this using apex?

  • 1
    Aggregate queries do not return individual records, but instead things like MIN, MAX, SUM, AVG, and COUNT values. You'd still be using a normal query, as explained in the answer. – sfdcfox Jan 16 '19 at 16:46

You an try following but it will count as 2 SOQL

Map<Id,Account> m = new Map<Id,Account> ([select id, name, (select id, name from Contacts) from account where id = ' your ACCOUNT ID']);

Account a = m.get('your ACCOUNT ID');

List<contact> cl = a.Contacts;

| improve this answer | |
  • Why does it count as 2 SOQL? – Robs Jan 16 '19 at 16:24
  • 1
    It's only one SOQL against the limits, not two. – sfdcfox Jan 16 '19 at 16:44

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.