Sub topic 1
What Agentforce Actions Are and When to Use Apex
An Agentforce agent talks with a user and decides what to do next. It cannot change a record, query data, or call another system by itself. It does those things through actions. This chapter explains what an action is, how the agent chooses one, which action types exist, and when Apex is the right choice. The rest of the tutorial builds an Apex action step by step.
Prerequisites
This chapter assumes the following:
- You know basic Apex: classes, collections, and SOQL.
- You know Salesforce objects, fields, and the sharing model (record access and field-level security).
- You know what an Agentforce agent and its topics are, at least at a high level.
- You have a Salesforce org with Agentforce enabled and Agent Builder available. You don't need it for this chapter, but later chapters use it.
What an action is
An action is a single unit of work that an agent can run. Each action has three parts:
- A name and a description. These tell the agent what the action does.
- Inputs. These are the values the action needs, such as a record Id or a date.
- Outputs. These are the values the action returns, such as a summary string or a count.
When a user asks for something, the agent does not run code directly. It reads the conversation, picks a topic, and looks at the actions assigned to that topic. It then chooses an action, fills in the inputs from the conversation, runs it, and writes a reply from the outputs.
An action is therefore a contract between two parties:
- The agent reads the contract in natural language and treats it as instructions.
- The platform runs the implementation behind the contract, which can be Apex, Flow, or another type.
How the agent uses actions
The reasoning engine follows a loop on each user turn. The exact internals can change between releases, but the working model is stable.
Three points in this loop affect how you write actions.
- The agent chooses from descriptions. If two actions have similar descriptions, the agent may pick the wrong one. If a description is vague, the agent may never pick the action.
- The agent fills inputs from the conversation. If an input is required and the user has not given it, the agent can ask a follow-up question. Input descriptions tell it what to ask for and in what format.
- The agent explains outputs. It does not show raw output to the user. It uses the output to write an answer, so outputs should be easy to read as text.
Later chapters cover descriptions and outputs in detail. For now, remember that an action is both code and prose. The prose is part of the interface.
Action types
Agent Builder lets you attach several kinds of actions to a topic.
| Action type | What it runs | Typical use |
|---|---|---|
| Standard action | Salesforce-provided logic | Common tasks such as summarizing a record, drafting an email, or querying records |
| Flow | An autolaunched flow | Declarative logic that admins can maintain |
| Apex | An Apex class with an `@InvocableMethod` | Logic that needs code: complex queries, calculations, or custom callouts |
| Prompt template | A prompt template sent to a model | Generating or transforming text from record data |
| API-based | An external API described by a schema | Calling a system that already has a documented API |
The set of types and their names change as Agentforce evolves, so check the current Salesforce documentation for your org's release. The important idea is that every type presents the same interface to the agent: a description, inputs, and outputs.
When to use Apex
Apex is a good fit when a declarative option cannot do the job cleanly. Ask these questions in order.
- Does a standard action already do this? If so, use it. You write no code and Salesforce maintains it.
- Can a flow do it in a readable way? If the logic is a few queries, a decision, and a record update, a flow is usually better. Admins can change it without a deployment.
- Is it text generation from record data? A prompt template is usually the better tool.
- Is it a call to an external API that has a schema? An API-based action may avoid code altogether.
- Does it need code? Choose Apex if any of these apply:
- The logic needs loops, maps, or algorithms that become hard to read as a flow.
- You need precise control over queries, sharing, or error handling.
- A flow would hit limits or become hard to test.
- You need a callout with custom authentication, parsing, or retry rules.
- You want unit tests that run in your deployment pipeline.
- The same logic is already in an Apex service class, and you want to expose it.
This is a guide, not a rule. Teams often pick Apex first because they know it, and then pay for it in maintenance. The reverse also happens: a flow grows to 60 elements and nobody can read it. Pick the simplest type that you can test and maintain.
What an Apex action looks like
An Apex action is an ordinary Apex class with one method marked @InvocableMethod. The method takes a list of request objects and returns a list of response objects. Each request and response class uses @InvocableVariable on its fields.
This skeleton shows the shape. It does nothing useful yet. The next chapters add real logic.
public with sharing class EchoAction {
public class Request {
@InvocableVariable(
label='Message'
description='Text the user wants repeated back'
required=true
)
public String message;
}
public class Response {
@InvocableVariable(
label='Echoed Message'
description='The same text that was provided'
)
public String echoed;
}
@InvocableMethod(
label='Echo Message'
description='Returns the text it receives. Used to demonstrate the action contract'
)
public static List<Response> echo(List<Request> requests) {
List<Response> responses = new List<Response>();
for (Request r : requests) {
Response res = new Response();
res.echoed = r.message;
responses.add(res);
}
return responses;
}
}Notice four things:
- The method is
public static, takes aList, and returns aList. - The class has one invocable method. Apex allows only one
@InvocableMethodper class. - Request and response types are inner classes with
@InvocableVariablefields. - The response list has one item per request, in the same order.
The final tutorial example is a GetOpenCasesAction. It lists open cases for an account with one query for all requests, runs in user mode, and returns one response per request. You will build it across the following chapters.
What makes Apex actions different from other Apex
Apex that runs from a trigger or a button is written for a developer or a system. Apex that runs as an agent action is called by a reasoning engine on behalf of a user. That changes how you write it.
- The caller is unpredictable. The agent may send null values, wrong formats, or ids for records the user cannot see. Validate inputs and return clear messages.
- The user's access matters. The action runs for a person who talked to the agent. It should respect that person's sharing, object permissions, and field-level security.
- The output is read by a model. A stack trace or an unhandled exception gives the agent nothing useful to say. Return short, plain text that the agent can turn into an answer.
- Descriptions are part of the code. A well-written method with a poor description will be misused or ignored.
- Calls can arrive in groups. The platform passes a list, so write for bulk even when you expect one request.
Common mistakes at this stage
- Choosing Apex too early. If a flow or standard action works, use it.
- Treating the description as a comment. The agent reads it to decide when to call the action. Write it for the agent, not for a future developer.
- Returning a single object. The method must return a list that matches the request list.
- Ignoring sharing. Declare
with sharingand, for queries, useWITH USER_MODE. Chapter 6 covers this in depth. - Assuming one request per call. Handle the list, even if you only ever see one item in testing.
What comes next
The next chapter looks at the @InvocableMethod annotation and its rules: which signatures are allowed, which parameters it accepts, and what the compiler rejects. After that, you model inputs and outputs, write descriptions the agent can use, and then build up bulk handling, security, error handling, testing, registration in Agent Builder, and deployment.
Exercise
A team wants an agent to send a customer a short thank-you message that uses the customer's name and their last order date, taken from two fields on a record. A second team wants an agent to check three external inventory systems, merge the results with custom rules, and handle timeouts in a specific order. Which action type fits each need best, and why?
Show answer
The first need fits a prompt template, because it generates text from record data and needs no custom logic. A flow could also work if the message is fixed. The second need fits Apex, because it needs custom callout handling, merging rules, and timeout control. Those are hard to express and test in a flow or a prompt template. In both cases a standard action should be checked first, in case it already covers the task.
Exercise
Name three properties of an Apex action's method signature and class that you saw in the EchoAction example.
Show answer
The method is public and static. It takes a List of request objects and returns a List of response objects, with one response per request in the same order. The class has only one @InvocableMethod. The request and response types use @InvocableVariable on their fields.