What Is Apex?
By the end of this lesson, you'll be able to:
- Explain what Apex is and where it runs
- Recognize when Apex is the right tool instead of a declarative feature like Flow
- Understand that Apex executes within Salesforce's governor limits
- Read a minimal Apex class and explain what each line does
Prerequisites: Module 1: "What Is Salesforce?"
What is Apex?
Apex is a strongly-typed, object-oriented programming language built directly into the Salesforce Platform. Its syntax is deliberately close to Java, so most experienced developers feel at home within a day or two — but this course assumes you've never programmed before at all, so we won't lean on that comparison to explain anything.
Unlike a language you'd run on your own server, Apex executes on Salesforce's infrastructure, alongside every other customer's code, on a shared, multi-tenant platform. That single fact shapes almost everything about how Apex is written — which is why later modules spend so much time on collections, bulk-safe patterns, and governor limits.
A real-world analogy
If Salesforce's data (Accounts, Contacts, your custom objects) is the furniture in your rented office unit, Apex is how you tell the room to react to things: "when someone drops off a package (a new record is created), automatically notify the front desk (send an email) and log it in the visitor book (update a related record)."
Flow can handle a lot of "when X happens, do Y" logic too — and Salesforce always wants you to try that first. Apex earns its place when the "do Y" part needs real logic: loops, math, calling an outside system, or handling hundreds of records at once cleanly.
When should you reach for Apex?
Salesforce always recommends starting with declarative tools — Flow, validation rules, formula fields — before writing code. Apex earns its place when you need:
- Complex business logic with loops, conditionals, or math that Flow can't cleanly express
- Operations across large numbers of records (bulk data processing)
- Custom integrations with external systems over REST or SOAP
- Triggers that need fine-grained control over when logic runs during a save
Module 2 goes deeper into this decision — for now, just know that "Apex vs. Flow" is a real, everyday decision professional Salesforce developers make, not a given.
Common mistakes
- Thinking Apex is "just like Java" and skipping the fundamentals. Apex borrows Java's syntax, but the platform it runs on — with governor limits, no explicit memory management, and deep integration with sObjects — makes it a genuinely different environment. Learn Apex on its own terms.
- Reaching for Apex by default. New developers sometimes write a trigger for something a validation rule or Flow would have handled in five minutes, with far less to maintain. Every module from here on will remind you to ask "could this be declarative?" first.
Best practices
- Keep this question in your back pocket for the rest of the course: "Is there a simpler, declarative way to do this?" If yes, prefer it.
- Read error messages carefully — Apex's compiler and runtime errors are usually specific about what went wrong and on which line.
Trade-offs and when NOT to use this
Apex is not free — every line of it executes inside Salesforce's shared, governed environment, which means it counts against limits (Module "Governor Limits and Bulk-Safe Code" covers this in depth) and it has to be tested and maintained by a developer, unlike a Flow an admin can adjust visually. If a business requirement can be met declaratively, that's usually the lower-maintenance, lower-risk choice — save Apex for where its power actually pays for its cost.
A minimal Apex class
public class HelloWorld {
public static void sayHello() {
System.debug('Hello, Salesforce!');
}
}
public class declares the class and makes it visible outside its own file. static means you can call sayHello() directly on the class (HelloWorld.sayHello()) without first creating a HelloWorld object. System.debug() writes a line to the debug log — Apex's equivalent of printing to a screen, which you'll use constantly while learning.
Exercise
Open the Developer Console, go to Debug > Open Execute Anonymous Window, and run: HelloWorld.sayHello(); — then check the debug log for your message.
Show hint
Look for a line starting with USER_DEBUG in the log output.
Exercise
Change the message inside System.debug('Hello, Salesforce!') to a message of your own, then run HelloWorld.sayHello(); in Execute Anonymous again and confirm your new message shows up in the log.
Show hint
Only the text between the quotes needs to change — everything else about the class stays exactly the same.
Exercise
Looking at the HelloWorld class above, answer in your own words: what is the class's name, what is the method's name, and which single line actually produces output you can see in the log?
Show hint
You don't need to understand every keyword yet — just point to the right pieces. The class name appears right after the word class; the method name appears right before the parentheses ().
What Is Apex? Quiz
Leaderboard
- 1. Monde Masiza 3/3
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Apex is Salesforce's strongly-typed, object-oriented programming language. It runs on the Lightning Platform itself, looks a lot like Java, and is the tool you reach for once declarative automation (like Flow) can't express the logic you need.