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:
- Query the Account by its saved Id and print
Before update:with its Description. - Set Description to
Reviewedand update that same Account. - Query it again by the saved Id into
saved, after the update. Any bind variable name is fine. - Print
Updated count:,After update:andName still tagged:(does the Name start withAS-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.
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 ›