Sub topic 1
What Agentforce Actions Are and When to Use Apex
Prerequisites
This chapter assumes you have the following:
- Basic Apex syntax: classes, collections, and SOQL.
- Familiarity with Salesforce objects, fields, and sharing.
- Awareness of what an Agentforce agent and its topics are.
- Access to an org with Agentforce enabled and Agent Builder available.
You do not need to write any code yet. This chapter builds the mental model that the rest of the tutorial relies on.
What an action is
An Agentforce agent talks with a user and decides what to do next. It cannot read records or change data by itself. It does that work through actions.
An action is a unit of work the agent can run. Each action has a name, a description, a set of inputs, and a set of outputs. The agent's reasoning engine reads the description and the input definitions, decides whether the action fits the user's request, and fills in the inputs from the conversation. Salesforce runs the action and returns the outputs. The agent then uses those outputs to write its reply.
Actions are grouped under topics. A topic is a category of work, such as case management, with its own instructions and its own list of actions. The agent first picks a topic, then picks an action from that topic.
Types of actions
You can build an action on several kinds of logic. Each kind suits different work.
| Action type | Best for | Trade-off |
|---|---|---|
| Flow | Record updates, approvals, and logic that admins maintain | Complex branching and data shaping get hard to read |
| Apex | Complex logic, custom queries, calls to external systems, data shaping | Needs a developer, tests, and deployment |
| Prompt template | Generating text from record data | Produces text, not data changes |
| Standard action | Common tasks Salesforce already provides | Limited to what the platform offers |
Where Apex fits
Choose Apex when a declarative option would be awkward or unsafe. Typical reasons are:
- The logic needs loops, maps, or string handling that Flow handles poorly.
- You need a query that joins several objects and then reshapes the result.
- You must call an external system with custom headers, retries, or response parsing.
- You need strict control over the output text so the agent receives clean, predictable data.
- You want unit tests that prove the behavior before the agent ever uses it.
Prefer Flow when an admin team will maintain the logic and it fits comfortably in a few elements. Prefer a standard action when one already does the job. Writing Apex you do not need adds code to test, deploy, and maintain.
How Apex becomes an action
Apex does not become an action by itself. You expose a method through the @InvocableMethod annotation, and you describe its inputs and outputs with @InvocableVariable. Agent Builder can then register that method as a custom action.
The class you will build across this tutorial follows this shape:
- A
Requestclass holds the inputs. Each field is an@InvocableVariable. - A
Responseclass holds the outputs, also with@InvocableVariablefields. - One static method takes a
List<Request>and returns aList<Response>, in the same order.
The method signature always uses lists, even when the agent calls it for one request. The platform is built for bulk callers such as Flow, so your code must handle many requests at once. A later chapter covers this in detail.
What makes an Apex action work well
Three ideas run through the rest of the tutorial.
- The contract is the interface. The agent sees only labels, descriptions, and variable definitions. Poor descriptions lead to wrong action choices or wrong inputs.
- The action runs as a normal Apex caller. Governor limits apply, and sharing and field-level security must be enforced deliberately in your code.
- The agent reads the output. Return clear text and counts, including a readable message when nothing is found, so the agent can explain the result.
Check your understanding
Exercise
A team wants an agent action that finds a customer's open cases, reshapes the results into a short summary, and runs with unit tests. Should they choose Flow, Apex, or a prompt template, and why?
Show answer
Apex is the better fit. The work needs a custom query, data shaping, and unit tests, which Apex handles directly. A prompt template generates text and does not run custom queries. A Flow could do it, but reshaping and testing the result is harder there.