ApexSensei

Drills DML operations

Upsert: insert or update

upsert inserts when no match exists and updates when one does.

Your task

Upsert the same tagged Account twice with Database.upsert: the first call inserts and the second updates.

String tag = 'AS-UPSERT-' + UserInfo.getUserId() + '-' +
    String.valueOf(Datetime.now().getTime());
Account acct = new Account(Name = tag, Description = 'First pass');

Database.UpsertResult firstResult;
Id insertedId;
Database.UpsertResult secondResult;
List<Account> matches = new List<Account>();

Requirements

  1. Database.upsert(...) should be present
  2. .isCreated() should be present
  3. Id = :insertedId should be present
  4. The first result should be printed as First upsert created: true
  5. The second result should be printed as Second upsert created: false
  6. The single row should be printed as Match count: 1
  7. The second Description should be printed as Saved description: Second pass

Sign in to open this drill