Introduction to Salesforce & Apex
Salesforce is a cloud platform best known for CRM (Sales Cloud, Service Cloud, Marketing, Commerce, Experience Cloud), but underneath every product is the same metadata-driven platform (historically called Force.com, now simply the Salesforce Platform).
1.1 The Salesforce Platform
Salesforce is a cloud platform best known for CRM (Sales Cloud, Service Cloud, Marketing, Commerce, Experience Cloud), but underneath every product is the same metadata-driven platform (historically called Force.com, now simply the Salesforce Platform). It provides:
A relational database exposed as objects (tables) and fields (columns): standard objects (Account, Contact, Opportunity, Case) and custom objects (Invoice__c).
Declarative tools (clicks, not code): Flow, validation rules, formula fields, page layouts, Lightning App Builder.
Programmatic tools: Apex (server side), Lightning Web Components (client side), Visualforce (legacy UI), APIs (REST, SOAP, Bulk, Streaming, GraphQL, Metadata, Tooling).
Security model: profiles, permission sets, roles, sharing rules, field-level security.
1.2 Multi-tenancy
Salesforce is multi-tenant: thousands of customers ("tenants") share the same infrastructure, database, and application servers. Each customer's environment is an org, isolated logically by an Org ID. Customizations are stored as metadata, not as separate code bases, which is why Salesforce can upgrade everybody three times a year.
Because resources are shared, the runtime enforces governor limits — hard per-transaction ceilings on SOQL queries, DML statements, CPU time, heap size, and more. One badly written loop cannot be allowed to slow down other tenants. Governor limits shape almost every design decision in Apex (§21).
┌─────────────────────── Shared Salesforce instance (e.g., a Hyperforce pod) ───────────────────────┐
│ Org A (metadata + data) Org B (metadata + data) Org C (metadata + data) ... │
│ ─────────────────────────────────────────────────────────────────────────────────────────────── │
│ Shared runtime: Apex engine, query optimizer, database, search, cache, async job queues │
└────────────────────────────────────────────────────────────────────────────────────────────────────┘1.3 What is Apex?
Apex is a strongly typed, object-oriented, Java-like language that runs on Salesforce servers (never in the browser). Key characteristics:
Classes, interfaces, generics-style collections, try/catch, for loops
Account, account, and ACCOUNT are the same identifier SOQL/SOSL queries and DML are part of the language, checked at compile time Hosted & compiled
Code is compiled and stored as metadata in your org; it runs on the multi-tenant runtime
Each request is a transaction; unhandled exceptions roll back all DML
Governor limits enforce fair resource use Production deployments require ≥ 75% code coverage and all tests passing
Every class/trigger is pinned to an API version (e.g., 65.0) for backward compatibility
1.4 When to use Apex (and when not)
Salesforce recommends "clicks before code". Use Flow and declarative tools when they are sufficient; reach for Apex when you need:
Complex business logic, loops over large data sets, or transactional control (savepoints).
Integrations: HTTP callouts, exposing REST/SOAP endpoints.
Asynchronous and batch processing of millions of records.
Custom controllers for LWC, Aura, and Visualforce.
Reusable invocable actions for Flow and Agentforce.
Fine-grained performance tuning that Flow cannot express.
1.5 A brief history
Salesforce founded; CRM delivered as SaaS Apex announced (Dreamforce 2006) and released as the platform's server-side language
Visualforce (page markup with Apex controllers) Batch Apex and Scheduled Apex
Aura-based Lightning Components; Queueable Apex Platform Events; Salesforce DX (CLI, scratch orgs, source-driven development)
switch statement; Change Data Capture; Lightning Web Components (2019)
Security.stripInaccessible GA; safe navigation operator ?. (Winter '21)
Transaction Finalizers; Assert class; External Credentials; Database.queryWithBinds
User-mode database operations (WITH USER_MODE, as user) GA; DataWeave in Apex; Code Builder
Null coalescing operator ??; UUID class; Apex Cursors (beta); Agentforce launched
Agentforce actions built on Apex; Salesforce Code Analyzer v5; continued API versions 63.0–66.0+