ApexSensei

Drills DML operations

Insert an Account with Contacts

A child record needs its parent's Id, so the parent Account is inserted before its Contacts.

Your task

Insert one tagged Account and three Contacts that belong to it, using exactly two DML statements.

String tag = 'AS-INSERTC-' + UserInfo.getUserId() + '-' +
    String.valueOf(Datetime.now().getTime());
String tagPattern = tag + '-%';
List<String> lastNames = new List<String>{ 'Ito', 'Park', 'Diaz' };
Integer dmlBefore = Limits.getDmlStatements();
Account parent = new Account(Name = tag);
List<Contact> contacts = new List<Contact>();

Requirements

  1. insert parent; should be present before any Contact is built
  2. AccountId = parent.Id should be present on each Contact
  3. (SELECT Id FROM Contacts) should be present in the Account query
  4. Contacts linked: 3 should be printed
  5. Orphan contacts: 0 should be printed
  6. DML used: 2 should be printed

Sign in to open this drill