Process Automation, Logic, and Integration27% of the exam

Dynamic Apex: describes, dynamic SOQL and Type

Pick the dynamic Apex feature a requirement needs: sObject tokens and describes, dynamic SOQL bind rules and Database.queryWithBinds, SOQL injection defences, dynamic DML with get, put and getSObject, Type.forName for plug-in classes, and dynamic SOSL.

Official exam objective
Given a scenario and requirements, identify the appropriate dynamic Apex feature to use in the solution.

~7 min read · 8 practice questions ·

What the exam expects

  • Choose the dynamic Apex feature for a requirement whose object, field, class or filter is only known at run time.
  • Get sObject tokens and describe results: from a record Id, from an object name in a variable, record type IDs by developer name, active picklist values.
  • Write dynamic SOQL that compiles and runs: simple bind variables, Database.queryWithBinds, and injection-safe user input.
  • Create and read records generically with newSObject, get, put and getSObject.
  • Instantiate a class named in configuration with Type.forName, and read dynamic SOSL results.

Key terms: token (a lightweight, compile-time-validated reference to an sObject or field, such as Account.SObjectType), describe result (DescribeSObjectResult or DescribeFieldResult, the metadata behind a token), dynamic SOQL/SOSL (a query string built and run at run time), dynamic DML (creating and saving records of a type chosen at run time), SOQL injection (user input that changes a dynamic query's structure).

Key facts

Record Id to object
Detail
Id.getSObjectType() returns the token; no query
Object name to token
Detail
Schema.getGlobalDescribe().get(name); keys are case-insensitive
New record of run-time type
Detail
token.newSObject()
Record type by developer name
Detail
getRecordTypeInfosByDeveloperName()
Picklist values
Detail
getPicklistValues() returns active entries only
Bind field in Database.query
Detail
Not supported: "Variable does not exist"
Database.queryWithBinds
Detail
API 57.0+; binds from a map; access level argument
escapeSingleQuotes
Detail
Adds \ before each single quote in user input
Parent via lookup
Detail
getSObject('Account'); get() reads fields on the record itself
Type.forName from a package
Detail
Use forName('', name) for a subscriber class in a no-namespace org
Dynamic SOSL results
Detail
One list per object, in RETURNING order
Sources for this section (9)
  • Why does `:filter.Priority__c` fail in `Database.query` when it works in inline SOQL?
  • When should code compare tokens instead of querying to learn a record's object?
  • Why must a managed package pass an empty namespace to `Type.forName` for a subscriber class?
  • What does `results[0]` hold after `Search.query` with `RETURNING Account(Id), Contact(Id)`?

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

Keep reading with the Platform Developer II pack

  • Core concepts
  • Exam traps
  • Worked scenario

The pack includes:

  • All 28 lessons of the Platform Developer II guide, with reading progress
  • Interactive decision walks and step-by-step sequence steppers
  • Inline practice questions and one-click topic practice (8 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 II exam