Sub topic 9
Choosing between Flow, Apex, prompt template, and standard actions
This chapter assumes you know what a topic is and that a topic can hold actions. You should also know basic Flow and Apex terms (autolaunched flow, invocable method). It covers how to pick an action type for a task, and what goes wrong when you pick badly.
Why the action type matters
An action is a unit of work the agent can run from inside a topic. The agent picks an action by reading its name and description. It does not read the implementation. From the agent's side, a Flow action and an Apex action look the same: a name, a description, inputs, and outputs.
The action type still matters to you, because it decides:
- who can build and maintain the action (admins or developers)
- how much logic fits inside it
- whether it reads data, changes data, or writes text
- how you test it and how it fails
Choose the simplest type that does the job, and keep the work inside one action small enough to describe in a sentence.
The four action types at a glance
| Type | What it is | Best for | Typical owner |
|---|---|---|---|
| Flow | An autolaunched flow exposed to the agent | Multi-step record work, such as queries, updates, and creates, with branching | Admin |
| Apex | An invocable Apex method | Custom logic, complex queries, calculations, calls to external systems | Developer |
| Prompt template | A template built in Prompt Builder that asks a language model to generate text from grounded data | Summaries, drafts, and rewrites that need generated text | Admin or developer |
| Standard action | An action Salesforce provides | Common jobs that need no build work, such as querying records, summarizing a record, or drafting an email | Nobody. You configure it. |
The exact list of standard actions varies by release. Check the action library in Builder instead of relying on a memorized list.
Flow actions
Use a Flow when the task is a sequence of record operations and your team already builds in Flow. Good examples are creating a case, updating a field after a check, or looking up a record and returning two or three of its fields.
Rules that cause most problems:
- The flow must be autolaunched. A screen flow needs a user interface, and an agent has none to show.
- Mark each variable the agent should fill as available for input. Mark each variable the agent should read as available for output. Variables left unmarked are invisible to the agent.
- Give every variable a clear description. The agent uses these descriptions to decide what to put in each input.
- Handle failures inside the flow. Return a clear value, such as
Not found, instead of letting the flow fault. The topic instructions can then say what to do with it.
Flow is a good first choice because an admin can change it without a deployment. It becomes awkward when the logic needs many nested loops, heavy data shaping, or careful governor-limit handling.
Apex actions
Use Apex when Flow would be unwieldy or cannot do the job. Typical reasons:
- Complex queries or aggregations
- Calculations with rules that are easier to test in code
- Callouts to an external service
- Logic that needs unit tests with tight coverage
An Apex action is an invocable method. It takes a list of request objects and returns a list of response objects. Each field the agent needs to see carries an @InvocableVariable annotation with a label and a description.
public with sharing class GetOpenCaseCount {
public class Request {
@InvocableVariable(label='Account Id' description='The Salesforce Id of the account to check' required=true)
public Id accountId;
}
public class Response {
@InvocableVariable(label='Open Case Count' description='Number of open cases on the account')
public Integer openCases;
}
@InvocableMethod(label='Get Open Case Count' description='Counts the open cases for one account')
public static List<Response> getCount(List<Request> requests) {
Set<Id> accountIds = new Set<Id>();
for (Request r : requests) {
accountIds.add(r.accountId);
}
Map<Id, Integer> counts = new Map<Id, Integer>();
for (AggregateResult ar : [
SELECT AccountId acc, COUNT(Id) total
FROM Case
WHERE AccountId IN :accountIds AND IsClosed = false
WITH USER_MODE
GROUP BY AccountId
]) {
counts.put((Id) ar.get('acc'), (Integer) ar.get('total'));
}
List<Response> results = new List<Response>();
for (Request r : requests) {
Response res = new Response();
res.openCases = counts.containsKey(r.accountId) ? counts.get(r.accountId) : 0;
results.add(res);
}
return results;
}
}This method runs one query for the whole list of requests, which keeps it bulk-safe. with sharing and WITH USER_MODE make the query respect the running user's record access, field-level security, and object permissions. Skipping both is the most common safety mistake in Apex actions, because the agent could then expose data the user should not see.
Prompt template actions
A prompt template action produces text. You write a template in Prompt Builder, ground it with record fields or other data, and the model generates the output. The agent then uses that text in its reply or passes it to another step.
Choose a prompt template when the output is language, for example:
- a summary of a case history
- a draft reply in the company's tone
- a rewrite of a description for a customer
Do not choose it for exact answers. A template cannot reliably compute a total, check an eligibility rule, or return a status. A Flow or Apex action returns the same answer every time for the same data. A prompt template returns generated text, which can vary. A good pattern is to have a Flow or Apex action fetch the facts and a prompt template turn them into prose.
Standard actions
A standard action is already built. You add it to a topic, review its inputs and outputs, and write instructions for when to use it. Use one first when it fits, because you have nothing to build, test, or maintain.
Limits to watch for:
- You cannot change the logic. If the behavior is close but not right, build your own Flow or Apex action.
- Make sure the description in your topic matches what the action really does, so the agent does not use it for the wrong request.
- Review which objects and fields it touches before enabling it.
A decision path
Read the path as a default order, not a rule. If your team has no Apex developers, a slightly clumsy Flow may still be the better choice. If a Flow would need dozens of elements, Apex is easier to read and test.
Worked example: three requests, three choices
Consider an agent for a store. Each request calls for a different type.
- "Where is my order?" This is a lookup with a single exact answer. Use a Flow that returns the status, or the Apex method from the topic examples in this tutorial if you want unit tests.
- "Summarize my last three support cases." The output is text. Use a prompt template grounded in the case records. If a standard summarize action covers it, use that instead.
- "How many open cases does this account have, counting only high priority?" This needs a filtered aggregate. Use Apex, like the example above.
For each action, the topic instructions should say when to run it and what to do with the result. The type does not change that part:
1. Ask for the account if the customer has not given one.
2. Use the Get Open Case Count action with the account Id.
3. If the count is 0, say there are no open cases.
4. Otherwise, state the count in one sentence.Common errors
- Choosing by habit. Writing Apex for a two-field lookup, or forcing a long calculation into Flow. Start from the decision path.
- Vague descriptions. If two actions are described as "gets account info" and "looks up account details," the agent will choose between them unpredictably. Make each description say what it returns and when to use it.
- Doing too much in one action. An action that looks up an order, issues a refund, and sends an email is hard to describe and hard to test. Split it, and let the topic instructions order the steps.
- Unmarked variables. A Flow input that is not available for input never gets filled. An Apex field without
@InvocableVariableis never seen. - Using a prompt template for exact facts. Fetch the facts with Flow or Apex first.
- Ignoring permissions. Flow run context and Apex sharing both affect what data comes back. Check that the action returns only what the running user may see.
Exercise
A topic must answer "Write a friendly reply to this customer about their delayed shipment," using the order's ship date and carrier. Which action type or types would you use, and why?
Show answer
Use a prompt template for the reply, because the output is generated text in a chosen tone. If the ship date and carrier are not already available as grounded fields in the template, add a Flow or Apex action to fetch them first. Facts come from the deterministic action, and the prompt template turns them into prose.
Exercise
You build a Flow action, but the agent never passes the order number into it. Name two likely causes.
Show answer
The input variable is not marked as available for input, or the variable has no description, so the agent cannot tell what value belongs there. It could also be that the flow is not autolaunched, or that the topic instructions never tell the agent to collect the order number first.