Sub topic 1
What Apex Is and Where It Runs
Prerequisites
This chapter assumes you understand Salesforce objects, fields, records, and basic Setup navigation. You should also know the basics of building a simple Flow and understand variables, loops, and collections.
What Apex Is and Where It Runs
Apex is Salesforce’s strongly typed, object-oriented programming language. It runs on Salesforce servers and can read, create, update, and delete Salesforce records. Apex also supports custom business logic that is difficult to express with declarative tools.
Apex commonly runs in these contexts:
- A trigger runs automatically when records are inserted, updated, deleted, or undeleted.
- A class contains reusable logic that other Apex code or automation can call.
- An invocable method exposes a class method as an action in Flow.
- A scheduled or asynchronous operation runs work later or outside the original request.
Apex runs inside a Salesforce transaction. A transaction is one unit of work, such as saving a batch of records. The transaction includes limits on database queries, record changes, and other resources. These limits protect the shared platform and affect both Apex and Flow.
What Flow Is and the Flow Types
Flow is Salesforce’s declarative automation tool. Declarative means you configure behavior with a visual builder instead of writing most of the implementation code.
Common Flow types include:
- A record-triggered Flow runs when a record is created, updated, or deleted.
- A screen Flow guides a user through questions and actions.
- An autolaunched Flow runs without screens, often from another automation or an Apex action.
- A scheduled-triggered Flow starts at a specified time for records that meet its conditions.
Flow can update records, send notifications, call subflows, and invoke Apex actions. It still runs within Salesforce transactions and therefore shares relevant platform limits with Apex.
Where Flow Is the Right First Choice
Start with Flow when the requirement is clear, record-oriented, and supported by standard elements. Flow is often the better choice for:
- Setting fields when conditions are met.
- Creating a related record.
- Sending a straightforward notification.
- Guiding users through a repeatable screen process.
- Allowing administrators to maintain the logic.
Choose Apex when the requirement needs complex algorithms, advanced collection processing, specialized integrations, or behavior that Flow cannot express clearly.
Exercise
A requirement sets a field on an Account when its Annual Revenue exceeds a threshold. Which tool should you try first, and why?
Show answer
Try a record-triggered Flow first because the condition and field update are straightforward and can be maintained declaratively.