Process Automation and Logic28% of the exam

SOQL, SOSL and DML in Apex

How to write inline SOQL (bind variables, relationship queries, aggregates, FOR UPDATE) and SOSL in Apex, predict exactly what each returns, and choose between DML statements and Database methods with partial success.

Official exam objective
Given a scenario, write SOSL, SOQL, and DML statements in Apex, and assess what they will return.

~8 min read · 9 practice questions ·

What the exam expects

  • Write inline SOQL with bind variables, including IN :collection, and predict whether it returns a list, one sObject, an Integer or AggregateResult rows.
  • Choose dot notation (child to parent) or a subquery (parent to child) to fetch related records in one query.
  • Choose SOSL over SOQL when a text term must be found across several objects, and read its list of lists.
  • Choose between DML statements and Database methods, use allOrNone = false for partial success, and use upsert with an external ID.
  • Lock rows with FOR UPDATE and know what that costs.

Key terms: bind variable (an Apex variable prefixed with a colon in a query), relationship name (the name SOQL uses to walk a lookup), AggregateResult (the row type of aggregate queries), allOrNone (the Database method flag for partial success), external ID (a field upsert can match on).

Key facts

Query with no rows into a List
Detail
Returns an empty list, not null
Query into a single sObject
Detail
Zero or several rows cause a runtime exception
Unaliased aggregate key
Detail
expr0, expr1 ... in SELECT order
SOSL result
Detail
List of lists, one per RETURNING object, same order
SOSL default result cap
Detail
Top 2,000 matches by relevancy
Query limits per transaction
Detail
100 SOQL (200 async), 20 SOSL, 50,000 rows retrieved by SOQL
DML limits per transaction
Detail
150 statements, 10,000 records processed
allOrNone default
Detail
true: one failure fails every record
Upsert keys
Detail
Id, external ID field, or idLookup standard field
FOR UPDATE lock wait
Detail
10 seconds, then QueryException
Sources for this section (8)
  • Why does code test a query result with `isEmpty()` instead of comparing it to null?
  • When should a developer use SOSL instead of SOQL?
  • Why can a bulk load keep its valid rows with `Database.insert(records, false)` but not with `insert records`?
  • How do you read `SUM(Amount)` with no alias from an AggregateResult?

The questions are free to read. Answers with their sources and self-grading are included with the pack.

Keep reading with the Platform Developer pack

  • Core concepts
  • Exam traps
  • Worked scenario

The pack includes:

  • All 21 lessons of the Platform Developer guide, with reading progress
  • Interactive decision walks and step-by-step sequence steppers
  • Inline practice questions and one-click topic practice (9 questions for this lesson)
  • An official source cited for every claim
  • An AI tutor that answers from the guide
  • Unlimited timed practice exams

Practice for the Platform Developer exam