Lesson 15 of 18 · 10–15 min
Variables and Collections
A strong Apex foundation prevents small syntax lessons from turning into unsafe production patterns.
Learning sequence · data model first, security layers next
- What CRM and Salesforce Do
- The Salesforce Ecosystem and Major Clouds
- Org, App, Object, Field, and Record
- Standard and Custom Objects
- Common Standard Objects
- Field Types and Data Quality
- Objects, Fields & Records
- Lookup vs Master-Detail
- Many-to-Many Relationships and Junction Objects
- Profiles, Permission Sets & Object Access
- Field-Level Security
- OWD and Least-Privilege Record Access
- Opening Access: Role Hierarchy, Sharing Rules, Public Groups & Apex Sharing
- Apex First Steps
- Variables and Collections
- SOQL and DML Basics
- Why Asynchronous Apex Exists
- REST Callout Introduction
Your focus
Prepare for Senior / Lead Roles
CHECKPOINT · Checkpoint • Design
- Check platform limits, transaction boundaries, and production trade-offs.
Learning objective
Explain and apply variables and collections in a small Salesforce scenario.
The concept
Variables hold typed values. List preserves an ordered collection, Set keeps unique values, and Map provides values by key. Collection-first thinking is essential for triggers, queries, and bulk DML.
Worked example
Collect Account IDs in a Set, query matching records once, then index them in a Map.Read the requirement, identify the records and transaction, run the smallest example in a safe environment, inspect the result, and then add tests and bulk considerations before production use.
List, Set, and Map
apex
List<String> names = new List<String>{'Acme', 'Globex'};
Set<Id> accountIds = new Set<Id>();
for (Contact contact : Trigger.new) {
if (contact.AccountId != null) accountIds.add(contact.AccountId);
}
Map<Id, Account> accountsById = new Map<Id, Account>(
[SELECT Id, Name FROM Account WHERE Id IN :accountIds]
);Common mistake
Copying a single-record example into automation without considering collections, limits, errors, or testability.
Interview angle
Explain the platform reason behind the syntax and state how the approach behaves for many records.
Pause and apply
What changes when this operation handles 200 records instead of one?
Official references
Reviewed 2026-09-23. Examples are illustrative; no code is executed here.
Key takeaways
- Apex runs inside governed transactions.
- Collections and bulk operations are the normal production shape.
Practice next
No linked assessment is available for this lesson. Use the pause-and-apply question to check your reasoning.
Reading these lessons does not yet save completion. Challenge results continue to provide assessment evidence.