ApexSensei

Drills SOQL queries

Read a Contact's Account

A child-to-parent query uses dot notation.

Your task

Query three tagged Contacts, read each one's Account name through the relationship, and upper-case it without crashing on the Contact that has no Account.

String tag = 'AS-Q306-' + UserInfo.getUserId() + '-' +
    String.valueOf(Datetime.now().getTime());
Account parent = new Account(Name = tag);
insert parent;
List<Contact> seeded = new List<Contact>{
    new Contact(LastName = tag + '-LinkedA', AccountId = parent.Id),
    new Contact(LastName = tag + '-LinkedB', AccountId = parent.Id),
    new Contact(LastName = tag + '-Orphan')
};
insert seeded;
String contactPattern = tag + '-%';

Integer linkedCount = 0;
Integer orphanCount = 0;
Boolean parentNamesMatch = true;
String orphanParentName = 'unknown';
System.debug('Contacts with an Account: ' + linkedCount);
System.debug('Parent names match: ' + parentNamesMatch);
System.debug('Contacts without an Account: ' + orphanCount);
System.debug('Orphan parent name: ' + orphanParentName);

Requirements

  1. Contacts with an Account: 2 should be built from the loop
  2. Account.Name should be present
  3. toUpperCase( should be present
  4. Contacts with an Account: 2 should be printed
  5. Parent names match: true should be printed
  6. Contacts without an Account: 1 should be printed
  7. Orphan parent name: null should be printed

Sign in to open this drill