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.
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,putandgetSObject. - 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
- Detail
Id.getSObjectType()returns the token; no query
- Detail
Schema.getGlobalDescribe().get(name); keys are case-insensitive
- Detail
token.newSObject()
- Detail
getRecordTypeInfosByDeveloperName()
- Detail
getPicklistValues()returns active entries only
- Detail
- Not supported: "Variable does not exist"
- Detail
- API 57.0+; binds from a map; access level argument
- Detail
- Adds
\before each single quote in user input
- Detail
getSObject('Account');get()reads fields on the record itself
- Detail
- Use
forName('', name)for a subscriber class in a no-namespace org
- Detail
- One list per object, in RETURNING order
Sources for this section (9)
- Id Classdeveloper.salesforce.com (opens in a new tab)
- Accessing All sObjectsdeveloper.salesforce.com (opens in a new tab)
- Dynamic DMLdeveloper.salesforce.com (opens in a new tab)
- DescribeSObjectResult Classdeveloper.salesforce.com (opens in a new tab)
- DescribeFieldResult Classdeveloper.salesforce.com (opens in a new tab)
- Dynamic SOQLdeveloper.salesforce.com (opens in a new tab)
- SOQL Injectiondeveloper.salesforce.com (opens in a new tab)
- Type Classdeveloper.salesforce.com (opens in a new tab)
- Dynamic SOSLdeveloper.salesforce.com (opens in a new tab)
- 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