Query one Account
Query the org (SOQL) · Beginner · 8 min · Runs in your Salesforce Developer org · Apex Path
Scenario. A support script needs to re-read the exact Account it just created without touching customer data.
Create one tagged Account, select its Id and Name, and verify the exact record returned.
What you will learn
SOQL reads Salesforce records into Apex. Select only the fields you will use, and filter by the Id you just created so no other record can match.
List<Account> rows = [SELECT Id, Name FROM Account WHERE Id = :savedId];
Gotcha. Reading a field you did not select throws an error. Id is the exception: Apex always returns it, even when you leave it out of SELECT.
The task
Create one tagged Account, query it back by its Id, and check the fields you selected.
Examples:
- one inserted Account →
Basic count: 1 - the queried Name equals the tag →
Basic name matched: true - the queried Id equals the inserted Id →
Basic id matched: true
Requirements:
- Keep the starter's insert. It creates one Account named with this run's tag.
- Select
IdandNamefrom Account. - Filter by the inserted Account's Id, so no other Account can match.
- Print the row count and whether the Name and Id match. Print
Basic count:,Basic name matched:andBasic id matched:.
Starter code
String tag = 'AS-Q301-' + UserInfo.getUserId() + '-' +
String.valueOf(Datetime.now().getTime());
Account seeded = new Account(Name = tag);
insert seeded;
List<Account> matches = new List<Account>();
System.debug('Basic count: ' + matches.size());
System.debug('Basic name matched: false');
System.debug('Basic id matched: false');
When it passes
The Id-scoped query returns one Account whose Id and Name match the inserted record.
You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.