ApexSensei

Drills DML operations

Handle Database.SaveResult

Database.insert(records, false) turns on partial success: it returns one SaveResult per record instead of throwing.

Your task

Insert one valid and one invalid Account with partial success, then read the SaveResult list.

String tag = 'AS-SAVERESULT-' + UserInfo.getUserId() + '-' +
    String.valueOf(Datetime.now().getTime());
List<Account> accounts = new List<Account>{
    new Account(Name = tag + '-valid', Description = tag),
    new Account(Description = tag + '-missing-name')
};

Integer successCount = 0;
Integer failureCount = 0;
StatusCode firstError;

Requirements

  1. Database.insert(accounts, false) should be present
  2. REQUIRED_FIELD_MISSING should be absent from your code
  3. Both results should be printed as Result count: 2
  4. The valid insert should be printed as Success count: 1
  5. The invalid insert should be printed as Failure count: 1
  6. The status code should be printed as First error: REQUIRED_FIELD_MISSING

Sign in to open this drill