Salesforce Introduction to Apex Programming Language
Flow Builder and declarative tools handle most Salesforce automation without code. But some business requirements are too complex, too unique, or too performance-sensitive for point-and-click tools. That is where Apex comes in. Apex is Salesforce's own programming language — built specifically to run on the Salesforce platform and interact with its data.
What Is Apex?
Apex is a strongly typed, object-oriented programming language that runs on Salesforce servers — not on your local machine. It looks and behaves very similarly to Java. If you have written Java before, Apex will feel familiar. If you have not, Apex is a solid first programming language because it is purpose-built and well-documented.
Apex code runs inside Salesforce's cloud infrastructure. You write it in a browser-based editor (or an IDE), and Salesforce compiles and executes it on their servers. Your code has direct, native access to Salesforce data — no HTTP calls needed to read or update records.
The Recipe Book Analogy
DECLARATIVE (Flow, Validation Rules) = A microwave with preset buttons. Fast, easy, works for most meals. APEX = A full chef's kitchen with every tool. Takes skill, but you can cook anything — exactly the way you want.
When to Use Apex Instead of Flow
Salesforce recommends using declarative tools first. Reach for Apex when:
- The logic is too complex for Flow (nested loops with conditional branching across multiple objects)
- You need to call an external web service and handle the response dynamically
- You need precise control over transaction boundaries and error handling
- Performance requirements are critical and Flow would hit governor limits
- You are building a reusable component or an AppExchange-ready package
Your First Apex Code
Here is a simple Apex class that greets a user by name:
public class GreetingHelper {
public static String greetUser(String name) {
String message = 'Hello, ' + name + '! Welcome to Salesforce.';
return message;
}
}
Breaking this down:
public class GreetingHelper— defines a class named GreetingHelper that anyone can accesspublic static String greetUser(String name)— a method that takes a name as input and returns a text stringString message = ...— declares a variable called message and assigns a value to itreturn message;— sends the result back to whoever called this method
Apex Data Types
Every variable in Apex has a type that defines what kind of value it holds:
| Data Type | What It Holds | Example |
|---|---|---|
| String | Text | String city = 'Mumbai'; |
| Integer | Whole numbers | Integer count = 10; |
| Decimal | Numbers with decimal places | Decimal price = 4999.99; |
| Boolean | True or False | Boolean isActive = true; |
| Date | A calendar date | Date today = Date.today(); |
| DateTime | Date plus time | DateTime now = DateTime.now(); |
| Id | A Salesforce record ID | Id accId = '001Hs00000XaBC1'; |
| List | An ordered collection of items | List<String> names = new List<String>(); |
| Map | Key-value pairs | Map<Id, Account> accMap = new Map<Id, Account>(); |
| Set | Unique values, no duplicates | Set<String> emails = new Set<String>(); |
Control Flow: If, For, While
Apex uses the same control structures as most programming languages:
// IF / ELSE
Integer score = 85;
if (score >= 90) {
System.debug('Grade: A');
} else if (score >= 75) {
System.debug('Grade: B');
} else {
System.debug('Grade: C');
}
// FOR LOOP
List<String> cities = new List<String>{'Delhi', 'Mumbai', 'Bengaluru'};
for (String city : cities) {
System.debug('City: ' + city);
}
// WHILE LOOP
Integer counter = 1;
while (counter <= 5) {
System.debug('Count: ' + counter);
counter++;
}
DML Operations: Working with Salesforce Records
In Apex, you interact with Salesforce data using DML statements — Data Manipulation Language. These are the commands that create, update, or delete records.
// CREATE a new Account Account newAcc = new Account(); newAcc.Name = 'Tata Motors'; newAcc.Industry = 'Automotive'; newAcc.Phone = '+91-22-6665-8282'; insert newAcc; // Saves the record to Salesforce // UPDATE an existing Account newAcc.AnnualRevenue = 2500000000; update newAcc; // DELETE a record delete newAcc;
DML statements count against Salesforce's Governor Limits — more on this in a later topic. The key rule: never put DML inside a loop. Doing so can hit the 150 DML statements per transaction limit very quickly.
System.debug(): Your Debugging Tool
System.debug() prints a message to the Debug Log — a log file you view in the Developer Console. Use it to inspect variable values and trace the flow of execution while building and testing your code.
Integer qty = 5;
Decimal unitPrice = 499.00;
Decimal total = qty * unitPrice;
System.debug('Total: ' + total); // Prints: Total: 2495.0
Where You Write Apex
- Developer Console — a browser-based IDE built into Salesforce. Access it from the gear icon → Developer Console.
- VS Code with Salesforce Extensions — the recommended IDE for professional development. Connects to your Salesforce org and provides code completion, linting, and deployment tools.
Key Points
- Apex is Salesforce's own object-oriented programming language, similar to Java, that runs on Salesforce servers.
- Use Apex when declarative tools cannot handle the complexity, performance, or integration requirements of a task.
- Core Apex data types include String, Integer, Decimal, Boolean, Date, Id, List, Map, and Set.
- DML statements (insert, update, delete) interact with Salesforce data — never place them inside loops.
- System.debug() writes to the Debug Log and is the primary tool for tracing and testing Apex code.
