Section 3 · Process Automation and Logic · 28% of exam
3.3 SOQL, SOSL, DML, and Working with Query Results
Exam objective
Given a scenario, select and interpret SOQL, SOSL, and DML operations, handle query results, and recognize safe query patterns.
SOQL: query structured records
SOQL retrieves records from known Salesforce objects. It supports field selection, filtering, sorting, limits, aggregates, and relationship queries. A query should return only fields and rows the process needs. For example, if a service receives a set of account IDs and must calculate open-case counts, it can query cases for all those IDs and group or accumulate the results. Querying all cases and filtering in Apex wastes rows and heap; querying each account separately risks exhausting query limits.
SOQL can retrieve child records from a parent query through a relationship subquery, or parent fields from a child query through relationship notation. Use the shape that makes the data requirement clear. A query assigned to a list can return zero or many records. A single-record query expression expects one row and can raise a query exception if no row or too many rows are returned. Match the result handling to the scenario's expected cardinality. Aggregate queries return grouped results rather than ordinary sObjects, so use the appropriate aggregate result type.
Bind variables separate user-supplied values from query text. This helps prevent injection when data values are used in filters. When a query must be dynamic, bind values where possible and validate dynamic object, field, and sort identifiers against an allowlist; identifiers cannot be secured by treating them as ordinary data values. Query selectivity also matters at scale: filters should narrow the result set, and the query plan and indexing strategy may become relevant in large data volumes.
SOSL: search indexed text
SOSL searches text across multiple objects and fields using the Salesforce search index. It is useful when a user provides a term and the application does not know which object contains the match. It is not a replacement for structured filters. If the scenario names an object and says “find all records where Status is Open and AccountId is in this set,” SOQL is the natural fit. If it asks to find a phrase across accounts, contacts, and opportunities, SOSL is more appropriate. SOSL returns search results grouped by object type. The code must handle the possibility that a particular object group has no matches. Search behavior depends on indexed fields and supported syntax, so do not assume that SOSL is an unrestricted scan of every field in the database.
DML: write records intentionally
DML statements insert, update, upsert, delete, undelete, and merge records. Use collection DML to process many records in one statement. Upsert uses an ID or external ID to decide whether to update an existing record or insert a new one. If partial success is required, Database methods can be called with allOrNone=false and return per-record results. Ordinary DML generally raises an exception when an operation fails, allowing the transaction to roll back if the failure is unhandled. Choose semantics from the requirement: should one bad row fail the whole operation, or should successful rows be retained while errors are reported individually?
Security is part of query and DML design. Salesforce platform behavior now includes API-version-specific defaults. Current documentation describes Apex database operations in user mode for newer API versions, including enforcement of the running user's object, field, and sharing permissions; older code and explicit system-mode operations may behave differently. Triggers remain a distinct execution context. For an exam question, follow the API version and security mode explicitly stated; for production work, inspect the class version and operation mode rather than assuming every Apex statement behaves identically.
A practical operation sequence
- Identify whether the task is structured selection, text search, or data modification. 2. Gather input IDs or search terms before database work. 3. Query only needed fields and rows, using relationships or grouping when useful. 4. Build changes in memory and issue collection DML. 5. Inspect errors according to all-or-none or partial-success requirements.
Concatenating untrusted text into dynamic SOQL can expose data or alter the query. Bind values and validate dynamic identifiers.
Extra concept: query results are not authorization
A query that returns a record does not necessarily mean the calling user should be allowed to see every field or perform every action. Evaluate sharing, object permissions, field-level security, restriction rules, and operation access mode separately from query syntax.
Key takeaways
- Use SOQL for structured queries on known objects and SOSL for indexed text search across object types.
- Select the correct result type and handle zero, one, and many rows deliberately.
- Use collection DML, bind variables, and explicit security behavior; partial-success operations require per-record result handling.
- Official references:
- Salesforce Certified Platform Developer Exam Guide: https://help.salesforce.com/s/articleView?id=005298965&language=en_US&type=1
- Working with data in Apex: https://developer.salesforce.com/docs/platform/apex-dev/references/apex-dev/apex-data-intro.html
- SOQL and SOSL query language: https://developer.salesforce.com/docs/platform/apex-dev/references/apex-dev/langcon-apex-soql.html
- Working with query results: https://developer.salesforce.com/docs/platform/apex-dev/references/apex-dev/langcon-apex-soql-working-with-results.html
- DML statements in Apex: https://developer.salesforce.com/docs/platform/apex-dev/references/apex-dev/langcon-apex-dml.html
- Apex user mode: https://developer.salesforce.com/docs/platform/apex-dev/references/apex-dev/apex-classes-enforce-usermode.html
- Dynamic SOQL and SOSL: https://developer.salesforce.com/docs/platform/apex-dev/references/apex-dev/apex-dynamic.html
