Apex fundamentals
Apex is the strongly-typed, object-oriented language that runs custom logic on Salesforce servers. Its syntax is deliberately Java-like, so classes, methods, and control flow feel familiar, but it runs in a governed, multitenant context that shapes how you write it. Apex is case-insensitive and every statement ends with a semicolon.
Primitive types include Integer, Long, Decimal, Double, Boolean, String, Date, Datetime, and Id. sObjects represent records: Account, Contact, and any custom __c object are types you can declare, instantiate, and manipulate. The three collection types are the backbone of bulk-safe code: List (ordered), Set (unique, unordered), and Map (key/value). Maps keyed by record Id are especially common for correlating records efficiently.
public class OpportunityService {
// Build a Map of Account Id -> its Opportunities, bulk-safely
public static Map<Id, List<Opportunity>> byAccount(List<Opportunity> opps) {
Map<Id, List<Opportunity>> result = new Map<Id, List<Opportunity>>();
for (Opportunity o : opps) {
if (!result.containsKey(o.AccountId)) {
result.put(o.AccountId, new List<Opportunity>());
}
result.get(o.AccountId).add(o);
}
return result;
}
}
Classes declare an access modifier — public, private, or global — and methods may be static (belonging to the class) or instance-level. System.debug() writes to the debug log, your primary tool for inspecting execution. Control flow uses the usual if/else, for, while, and the Apex-specific switch on.
The overriding habit to build is thinking in bulk. Apex frequently runs against up to 200 records at once (in triggers) or thousands (in batches), so write methods that accept and return collections rather than single records. Almost every governor-limit failure traces back to per-record work that should have been set-based.
As an exercise, write a class with a static method that takes a List<Account> and returns a Map<Id, Account>, then invoke it from anonymous Apex and inspect the debug log. With the language basics in place, the next lesson covers how Apex reads and writes data through SOQL and DML.