ApexSensei

Drills SOQL queries

Load Account Contacts

A parent-to-child subquery pulls Accounts and their Contacts in one SOQL.

Your task

Load the two tagged Accounts and their Contacts with one query that uses a Contacts subquery.

String tag = 'AS-Q307-' + UserInfo.getUserId() + '-' +
    String.valueOf(Datetime.now().getTime());
String tagPrefix = tag + '%';
List<Account> parents = new List<Account>{
    new Account(Name = tag + '-Busy'),
    new Account(Name = tag + '-Empty')
};
insert parents;
insert new List<Contact>{
    new Contact(LastName = tag + '-Beta', AccountId = parents[0].Id),
    new Contact(LastName = tag + '-Alpha', AccountId = parents[0].Id)
};

Requirements

  1. Child count should be computed from the list
  2. Child subquery should be present
  3. Accounts loaded: should be 2
  4. Busy Account contacts: should be 2
  5. Empty Account contacts: should be 0
  6. Contacts sorted: should be true

Sign in to open this drill