Sub topic 4
Actions: how an agent does work
Prerequisites
This chapter assumes you know basic Salesforce objects, records, and permission sets. It also assumes you have read Flow or Apex at a beginner level. You should have met topics and instructions in the previous chapter.
What an action is
A topic tells an agent what it should handle. An action is what the agent can actually do inside that topic. Without actions, an agent can only talk. With actions, it can read records, update them, send messages, or call other systems.
An action is a single, named unit of work with defined inputs and outputs. The agent's reasoning engine decides when to call it. The action itself runs ordinary Salesforce logic and returns a result. The agent then uses that result to write its reply.
Service agents and employee agents use the same action model. The difference is who the action runs as. The identity chapter covers that in detail.
Action types
Each action wraps a piece of existing Salesforce functionality. The common types are listed below.
| Action type | What it runs | Typical use |
|---|---|---|
| Flow | An autolaunched flow | Look up or update records, run a business process |
| Apex | An invocable Apex method | Custom logic, complex queries, calls to external services |
| Prompt template | A prompt template | Draft or summarize text from record data |
| Standard action | A prebuilt action that ships with Agentforce | Query records, summarize a record, answer from knowledge |
| API or external service | An external service registration or MuleSoft API | Reach systems outside Salesforce |
Pick the simplest type that does the job. A flow is usually easier for an admin to maintain. Apex fits cases where you need bulk-safe queries, complex branching, or code-level testing.
The parts of an action
Every action has the same parts, whatever its type.
- Name and description. The reasoning engine reads these to decide whether the action fits the customer's request.
- Inputs. The values the action needs. The agent can fill them from the conversation, from context such as the current record, or from earlier action outputs.
- Outputs. The values the action returns. The agent can quote them or pass them to another action.
- Instructions for the action. Guidance on when to call it and how to use the result.
- Target. The flow, Apex method, or prompt template that does the work.
Inputs and outputs need their own descriptions too. A description such as "Order Number" is weak. A better one is "The order number the customer provided, for example 00012345." The agent uses these descriptions to decide what to ask for and where to put each value.
How actions fit into a conversation
The agent loop runs on every customer message. The reasoning engine picks a topic, then looks at the actions in that topic. If an action fits, it collects the inputs, calls the action, and reads the result. It can then call another action or write a reply.
Two points follow from this loop. First, the agent never runs your code unless an action points to it. Second, the agent only sees what the action returns. If your action returns a bare status code, the agent has little to say beyond that code.
Writing an Apex action
An invocable Apex method becomes an action when you add it to a topic. The method needs the @InvocableMethod annotation. Its inputs and outputs are wrapper classes with @InvocableVariable fields.
public with sharing class GetOrderStatusAction {
public class Request {
@InvocableVariable(label='Order Number' required=true)
public String orderNumber;
}
public class Response {
@InvocableVariable(label='Order Status')
public String status;
}
@InvocableMethod(
label='Get Order Status'
description='Returns the status of an order for a given order number'
)
public static List<Response> getStatus(List<Request> requests) {
Set<String> orderNumbers = new Set<String>();
for (Request r : requests) {
orderNumbers.add(r.orderNumber);
}
Map<String, String> statusByNumber = new Map<String, String>();
for (Order o : [SELECT OrderNumber, Status FROM Order WHERE OrderNumber IN :orderNumbers]) {
statusByNumber.put(o.OrderNumber, o.Status);
}
List<Response> results = new List<Response>();
for (Request r : requests) {
Response res = new Response();
res.status = statusByNumber.containsKey(r.orderNumber)
? statusByNumber.get(r.orderNumber)
: 'Order not found';
results.add(res);
}
return results;
}
}Several details in this code matter for agents.
with sharing. The class respects the record access of the user it runs as. Leaving it out can expose records the user should not see.- Bulk-safe query. The method takes a list and runs one query for all requests. Invocable methods receive lists, so a query inside a loop would hit governor limits.
- Order of results. The method returns one response per request, in the same order. Invocable methods depend on that order.
- A defined "not found" value. The method returns
'Order not found'instead of throwing an exception. The topic instructions can then handle that exact value. labelanddescription. These are what the reasoning engine reads. Write them for the agent, not only for other developers.
Connecting the action to a topic
Adding the action to a topic is not enough. The topic's instructions should say when to call it and how to treat the result. The example below pairs with the Apex class above.
Topic: Order Status
Classification description: Customer asks where an order is or what state it is in.
Scope: Look up the status of a single order using the Get Order Status action.
Instructions:
- Ask for the order number if the customer has not given one.
- Call the Get Order Status action with that order number.
- Report only the status returned by the action.
- If the action returns 'Order not found', ask the customer to check the number once, then offer to connect them with a human agent.
- Do not discuss refunds or cancellations in this topic.The instructions name the action exactly. They state what to do before the call (ask for the number), what to do with the result (report only the status), and what to do on failure (ask once, then offer a human). Vague instructions such as "help with orders" leave these choices to the model.
An employee agent uses the same structure. Only the audience and data access change.
Topic: My Open Cases
Classification description: An employee asks about support cases assigned to them or their team.
Scope: Summarize cases the employee already has permission to view.
Instructions:
- Use the Get Open Cases action to retrieve cases for the current user.
- Present each case with its number, subject, and status.
- If the employee asks about data they cannot access, say that you cannot show it and suggest contacting their administrator.Design guidelines
- Keep each action narrow. One action should do one thing. An action called "Handle Order" that looks up, updates, and cancels is hard for the agent to use safely. Split it.
- Name and describe for the model. Use plain verbs and nouns. Say what the action returns.
- Return useful values. Return readable text or a clear status, not internal IDs the customer cannot use.
- Return failure as data. A defined value such as "Order not found" is easier to handle than an unhandled error.
- Separate reads from writes. Give read-only actions freely. Treat actions that change data with more care, and consider asking the customer to confirm before the agent calls them.
- Limit the actions per topic. A topic with many similar actions makes the choice harder. Group related actions and keep topics small.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The agent never calls the action | The description is vague, or the topic instructions do not mention it | Rewrite the description and name the action in the instructions |
| The agent asks for input it already has | The input description does not say it can come from context | Describe the input and its source clearly |
| The action returns no data for a valid record | The running user lacks access, or the class runs without sharing rules as intended | Check the permissions of the identity that runs the action |
| The agent invents details after an empty result | The instructions do not cover the empty case | Add an instruction for that result |
| Bulk errors in Apex | A query or DML statement sits inside a loop | Query once for the whole list, as in the example |
Testing an action
Test at two levels. First, test the target on its own. Run the flow in debug mode, or write an Apex test that covers found, not found, and bulk cases. Second, test the action through the agent in the testing tools, with phrasings customers might really use. The next chapters on the reasoning engine and testing cover this in more detail.
Exercise
An Apex invocable method for an action runs a SOQL query inside a for loop over the incoming requests. Why is this a problem, and how do you fix it? Also, what should the method return if no record matches?
Show answer
Invocable methods receive a list of requests, so a query in the loop can run many times and hit governor limits. Collect the keys into a set, run one query with an IN clause, store results in a map, then build one response per request in the original order. If no record matches, return a defined value such as 'Order not found' so the topic instructions can handle that case, instead of throwing an exception.
Exercise
A topic contains a Get Order Status action, but the agent answers order questions from general knowledge and never calls the action. Name two changes that are likely to fix this.
Show answer
Rewrite the action's name and description so they clearly match the customer's request, and add a topic instruction that tells the agent to call the Get Order Status action with the order number. Checking that the topic's classification description matches the request also helps.