ApexSensei

Drills › Change records (DML)

Update a tagged Account

Change records (DML) · Beginner · 12 min · Runs in your Salesforce Developer org · Apex Path

Scenario. A review step must change one record created by the current job without touching customer Accounts.

Create one tagged Account, update its Description, and read the record before and after to prove the change.

What you will learn

update saves changed fields on a record that already has an Id. Keep that Id, and read the row from the database before and after to see what changed.

account.Description = 'Reviewed';
update account;

Gotcha. Never pick a record to change with a broad query such as LIMIT 1. Always filter by an Id you own.

The task

Update one tagged Account's Description and prove the change by reading the record before and after.

Examples:

  • query the new Account before the update → Before update: Pending
  • query the saved Id after the update → Updated count: 1, After update: Reviewed, Name still tagged: true

Requirements:

  1. Query the Account by its saved Id and print Before update: with its Description.
  2. Set Description to Reviewed and update that same Account.
  3. Query it again by the saved Id into saved, after the update. Any bind variable name is fine.
  4. Print Updated count:, After update: and Name still tagged: (does the Name start with AS-UPDATE-?).

Starter code

String tag = 'AS-UPDATE-' + UserInfo.getUserId() + '-' +
    String.valueOf(Datetime.now().getTime());
Account account = new Account(Name = tag, Description = 'Pending');
insert account;
Id originalId = account.Id;

List<Account> saved;

When it passes

The same tagged Account reads Pending before the update and Reviewed after it, and keeps its Id and Name.

Try this drill free

You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.

‹ Insert a tagged Account · Delete challenge-created records ›