Beginner 22 min read

Apex Foundations for LWC Developers

By the end of this lesson, you'll be able to:

  • Explain the difference between client-side (LWC) and server-side (Apex) code
  • Read an Apex class and method signature, piece by piece
  • Recognize sObjects, List<T>, SOQL, and DML by sight
  • Explain what an exception is and why with sharing/@AuraEnabled/cacheable=true appear on Apex methods used by LWC

Prerequisites: Module 6: "Project: Account/Contact Management Without Apex"

What Is Apex, and Where Does It Run?

Every JavaScript example so far in this course — the LWC components themselves — runs client-side: in the user's own browser, on their own device. Apex is Salesforce's own programming language, and it runs server-side: on Salesforce's servers, before any result is sent back to the browser.

This distinction is exactly why Module 6 (Lightning Data Service) could do so much without Apex at all — reading and writing simple records doesn't need custom server-side code. The next lesson, "When Apex Is Required," covers precisely when that stops being true. This lesson's job is narrower: making sure the Apex code shown from here on is actually readable, syntax by syntax.

Apex Classes and Methods

public class AccountController {
    public static String getWelcomeMessage() {
        return 'Hello from Apex';
    }
}

This is the same class / method shape Module 3 taught for JavaScript — a class groups related methods together. The differences that stand out immediately, both covered next: public appears before class and before the method, and the method declares its return type (String) explicitly, right in the signature — Apex requires this; JavaScript never did.

public, private, and static

  • public — this class or method can be accessed from outside the class. private — the opposite: only usable from inside the same class. Apex methods that LWC needs to call are always public (in fact, @AuraEnabled methods must be public static, covered later in this lesson).
  • static — the method belongs to the class itself, not to a specific instance of it. You call it directly on the class name (AccountController.getWelcomeMessage()), without first needing to create an object from that class, similar in spirit to how you've been calling plain JavaScript functions rather than object methods.

Primitive Types

Unlike JavaScript, Apex requires every variable's type to be declared up front — Apex is a strongly typed language:

String companyName = 'Acme Corp';
Boolean isActive = true;
Id accountId = '001XXXXXXXXXXXXXXX';
Integer recordCount = 42;

String (text), Boolean (true/false — the exact same concept Module 3 taught, just capitalized here per Apex's naming convention), and Integer (a whole number) map directly onto the string/boolean/number value types already familiar from JavaScript. Id is new: a special Apex type specifically for a Salesforce record's unique identifier — every record of every object has one.

Reading a Method Signature, Line by Line

public static List<Account> getAccounts()

Breaking this apart piece by piece:

  • public — callable from outside this class (explained above).
  • static — called on the class itself, no instance needed (explained above).
  • List<Account> — the return type: this method gives back a List (Apex's ordered, resizable collection — conceptually the same job a JavaScript array does) specifically of Account records. <Account> is a type parameter, saying exactly what kind of item the list holds.
  • getAccounts — the method's name, chosen by the developer, exactly like naming a JavaScript function.
  • () — the parameter list. Empty here means this method needs no input to run; a method needing input would declare typed parameters here, e.g. (String searchTerm).

Every Apex method signature in the rest of this module follows this exact reading order: access modifiers, then return type, then name, then parameters.

sObjects and Lists

Account in the example above is an sObject type — Apex's built-in representation of a Salesforce record. Account, Contact, and Case are all standard sObject types, each with fields matching that object's real fields in Salesforce (Account.Name, Contact.Email, and so on). A List<T> is Apex's core collection type — an ordered, resizable list of items of a specific type, filling the same role a JavaScript array does, just with its item type declared explicitly (List<Account>, List<String>, List<Id>).

SOQL and DML, Briefly

List<Account> accounts = [SELECT Id, Name FROM Account WHERE Industry = 'Banking'];

SOQL (Salesforce Object Query Language) is how Apex reads records — a SELECT statement, written directly inside square brackets, embedded right in the Apex code. DML (Data Manipulation Language) is the small set of keywords — insert, update, delete — that write changes back to the database:

Account newAccount = new Account(Name = 'New Company');
insert newAccount;

Both are covered in real depth as this module continues — this is only enough to recognize them by sight in upcoming examples.

Exceptions

Apex has try/catch too, working the same way Module 3 taught it for JavaScript: an exception is an error that stops normal execution unless it's caught. A later lesson in this module covers AuraHandledException specifically — the deliberate exception type used to send a safe, custom error message back to LWC.

A Safe Default for Now: with sharing, @AuraEnabled, cacheable=true

Three pieces of syntax will appear on nearly every Apex class and method LWC calls, starting with the very next lesson — enough to use them safely now, with Module 12 providing the complete picture once the rest of this module's data/Apex foundations are in place:

  • public with sharing class ... — makes the class respect the running user's normal record-level access (which records they're allowed to see), a safe default for a class LWC calls into.
  • @AuraEnabled — the annotation that exposes an Apex method to Lightning components at all; without it, LWC cannot call the method.
  • cacheable=true — marks a method as read-only, required for it to be used with @wire (Module 6).

Seeing all three together on a method — @AuraEnabled(cacheable=true) above a public with sharing class's method — is the standard, safe shape for a simple Apex method LWC will read data from.

Exercise

Label each piece of this method signature: public static List<Contact> getContacts(String searchTerm)

Show hint

Follow the reading order this lesson taught: access modifier, static, return type, name, parameters.

APEX

Exercise

Challenge: explain, as a comment, why Boolean and String in Apex are conceptually the same idea already taught in Module 3 for JavaScript, despite the capitalized names.

Show hint

Think about what each value type represents, not how it happens to be spelled.

APEX

Apex Foundations for LWC Developers Quiz

1. Where does Apex code run?

2. What does the static keyword mean on an Apex method?

3. In public static List<Account> getAccounts(), what does List<Account> represent?

4. What is an sObject?

5. What does SOQL do?

6. What must be true of an Apex method for it to be callable from LWC at all?

Log in to submit the quiz and save your score.

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

Just enough Apex vocabulary to read the code in the rest of this module confidently — not a full Apex course, but enough that no Apex example ahead relies on syntax you haven't seen explained.