Sub topic 2
Choosing an Action Target: Flow, Apex, or Prompt Template
An action target is the piece of Salesforce logic that runs when the agent calls an action. This chapter helps you choose between a Flow, an Apex invocable method, and a prompt template. It also shows how that choice changes the inputs and outputs the agent sees.
This chapter assumes you can open Agentforce Builder and add an action to a topic. It also assumes you have built a basic autolaunched flow or written a simple Apex class. You do not need prior experience with prompt templates.
Why the target matters for input and output design
The agent never sees your Flow elements or your Apex code. It sees a name, a description, a list of inputs, and a list of outputs. Each target type produces that list in a different way.
- A Flow exposes the variables you mark as available for input and output.
- An Apex invocable method exposes the fields of its request and response classes.
- A prompt template exposes its template inputs and returns generated text.
So the target decides where you write labels and descriptions, what data types you can use, and what an output looks like. It also decides how much work you do to return a clear error. Pick the target first, then design the inputs and outputs inside its rules.
The three targets
Flow
Use an autolaunched flow when the work is a record operation or a short process that an admin can maintain without code. Typical examples are looking up a record, creating a case, updating a field, or calling a subflow.
How it exposes metadata:
- Each variable you mark Available for input becomes an action input.
- Each variable you mark Available for output becomes an action output.
- The variable's description is the main text the agent reads for that input or output. Fill it in on the variable, not only on the flow.
- Supported variable types include text, number, boolean, date, record, and collections of these.
Strengths: admins can change it without a deployment, and it handles ordinary CRUD (create, read, update, delete) well. Weaknesses: complex branching and data shaping get hard to read, and error handling needs explicit fault paths.
Apex
Use an invocable Apex method when you need logic that is awkward in a Flow. Examples are aggregations, calls to external services, complex parsing, or a result shaped for the agent.
How it exposes metadata:
- The
@InvocableMethodannotation provides the action label and description. - Each field with
@InvocableVariablebecomes an input or output, depending on whether it sits in the request or the response class. - The
label,description, andrequiredattributes on those annotations are what the agent reads. - A class can have only one invocable method, and the method takes and returns a
List.
Strengths: full control over output shape, messages, and error handling. Weaknesses: it needs a developer, a deployment, and tests, and the shape of the input and output classes is fixed once the action is in use.
Prompt template
Use a prompt template when the result is text that a large language model should write from grounded data. Examples are a summary of a case, a drafted reply, or a description built from record fields.
How it works:
- You build the template in Prompt Builder and reference record fields, related lists, or Flow and Apex results as merge content.
- The template's inputs become the action's inputs. A common input is a record, such as an account or case.
- The output is the generated text. You have little control over its structure.
Strengths: the template does language work that Flow and Apex cannot do well. Weaknesses: the output is free text, so it is harder to test for exact values. The inputs are usually whole records, not individual fields, so you must make sure the template grounds on the right data.
A prompt template does not look data up for you in the same way a query does. If the template needs data that is not on the input record, feed it with a Flow or Apex resource.
Comparing the targets
| Concern | Flow | Apex invocable method | Prompt template |
|---|---|---|---|
| Best for | Record lookups and updates, short processes | Custom logic, callouts, aggregation | Generating or summarizing text |
| Who maintains it | Admins | Developers | Admins and prompt designers |
| Where input text is written | Variable descriptions | `@InvocableVariable` attributes | Template input definitions and instructions |
| Output shape | Variables you define | Response class you define | Generated text |
| Predictable output values | High | High | Low |
| Error control | Fault paths | `try`/`catch` and message fields | Limited. Handle upstream. |
| Deployment effort | Low | Higher, with tests | Low |
A decision path
Work through these questions in order. Stop at the first one that fits.
Two practical tie-breakers:
- If an admin team owns the process and the logic fits in a Flow, choose Flow. Fewer moving parts means fewer places to break.
- If you need to return a specific message for each outcome, such as "no order found", Apex gives you the most direct control. A Flow can do it too, with a decision element and a text output variable.
Combining targets
You are not limited to one target per action. Common combinations:
- An action that runs a Flow, which calls an Apex invocable method for one hard step.
- A prompt template that pulls its grounding data from a Flow or Apex resource.
Keep the outer layer simple. The agent should see one action with a few well-described inputs and outputs, not the inner pieces.
Example: the same lookup in Apex
Suppose the agent needs to answer "How many open cases does this account have?" This is an aggregation over records, and the answer is a number. A Flow could do it with a Get Records element and a count, but the Apex version returns the number and a ready-to-read message in one step.
public with sharing class GetOpenCaseCount {
public class Request {
@InvocableVariable(
label='Account Id'
description='The Salesforce Id of the account, taken from the conversation context. Ask the user which account if it is not clear. Do not guess.'
required=true
)
public Id accountId;
}
public class Response {
@InvocableVariable(
label='Open Case Count'
description='Number of cases on the account that are not closed. 0 if there are none.'
)
public Integer openCases;
@InvocableVariable(
label='Result Message'
description='A short plain-language summary of the result.'
)
public String message;
}
@InvocableMethod(
label='Get Open Case Count'
description='Counts the open cases for one account. Use when the user asks how many open cases an account has.'
)
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> countByAccount = new Map<Id, Integer>();
for (AggregateResult ar : [
SELECT AccountId acct, COUNT(Id) total
FROM Case
WHERE IsClosed = false AND AccountId IN :accountIds
WITH USER_MODE
GROUP BY AccountId
]) {
countByAccount.put((Id) ar.get('acct'), (Integer) ar.get('total'));
}
List<Response> results = new List<Response>();
for (Request r : requests) {
Response res = new Response();
Integer total = countByAccount.get(r.accountId);
res.openCases = (total == null) ? 0 : total;
res.message = 'The account has ' + res.openCases + ' open case(s).';
results.add(res);
}
return results;
}
}Points to notice:
- The method takes a list and returns a list in the same order, even though the agent usually sends one request. This is required by the invocable method contract.
- The query uses
WITH USER_MODE, so the action respects the running user's permissions. - A missing count becomes
0, and the message says so. The agent does not have to interpret an empty value. - Every input and output has a label and a description written for the agent, not for a developer.
If you built the same thing as a Flow, you would set the accountId variable to Available for input, the count variable to Available for output, and write the descriptions on those variables. The agent would see the same kind of metadata.
Common errors when choosing a target
- Choosing Apex by habit. A Flow is easier for admins to read and change. Choose Apex only when Flow becomes awkward.
- Using a prompt template for lookups. A template generates text. If you need an exact value, such as an order status, return it from a Flow or Apex and let the agent phrase it.
- Forgetting the descriptions on Flow variables. An undocumented variable gives the agent only its API name to go on. The agent will guess how to fill it.
- Returning whole records when the agent needs one field. Extra data raises the chance the agent quotes the wrong value. Return only what it needs to answer.
- Hiding errors inside the target. If the Flow or Apex fails silently, the agent reports success. Return an explicit message for empty and error cases. Later chapters cover this in detail.
- Changing the target shape after launch. Renaming or removing an Apex field or Flow variable that an action already uses can break the action. Test the action again after any change.
Checklist before you build
- Decide whether the result is exact data or generated text.
- Pick the simplest target that can return it.
- List the inputs the agent must provide, and the outputs it needs to answer.
- Write the label and description for each one in the place your target requires.
- Decide what each failure and empty case will return.
Exercise
A user asks, "Summarize the last five emails on this case in two sentences." Another user asks, "What is the status of order 00000101?" Which target would you choose for each action, and why?
Show answer
Use a prompt template for the summary, because the result is generated text grounded in record data. Use a Flow or an Apex invocable method for the order status, because the agent needs an exact value from a record. A Flow is enough if it is a single lookup. Choose Apex if you want to return a specific message when no order matches. In both cases the agent can then phrase the answer to the user.
Exercise
You built an action as a Flow, and the agent keeps passing the wrong value to one input. The variable is marked Available for input, but its description is empty. What should you change first?
Show answer
Write a specific description on that Flow variable. It should say what the value is, give an example, and say what to do if the value is missing. The agent reads the variable's description to decide what to pass, so an empty description leaves it guessing from the variable name.