Sub topic 2
Choosing an action target: Flow, Apex, or prompt template
This chapter assumes you know the basics of Setup and Agentforce Builder, and that you can read a Flow variable or an Apex class. It helps you pick the kind of logic an action runs. The choice changes how you define inputs and outputs in later chapters.
What an action target is
An action is the unit of work an agent can call. The target is the thing that runs when the agent calls it. Agentforce supports three common targets:
- A Flow, built with Flow Builder and run as an autolaunched flow.
- An Apex invocable method, written in code.
- A prompt template, which asks a large language model to produce text from merged data.
From the agent's side, all three look the same. It reads a name, a description, a list of inputs, and a list of outputs. It never sees your implementation. This is why the metadata you write matters more than the target you pick.
How each target defines inputs and outputs
Each target exposes inputs and outputs in its own way.
| Target | Where inputs come from | Where outputs come from | Typical use |
|---|---|---|---|
| Flow | Variables marked "Available for input" | Variables marked "Available for output" | Record lookups, updates, and multi-step logic that admins maintain |
| Apex invocable method | Fields on a request class marked with `@InvocableVariable` | Fields on a response class marked with `@InvocableVariable` | Complex logic, custom queries, and calls to external systems |
| Prompt template | Template inputs, such as a record or text value | The generated text | Drafting, summarizing, and rewriting content |
In a Flow, the label and description of each variable become what the agent reads. In Apex, the label and description attributes on @InvocableVariable do the same job. In a prompt template, the template's input definitions and description play that role.
Choosing between them
Start with the simplest target that does the job.
- Use a Flow when the work is a record lookup, a record update, or a short sequence of steps. Admins can read and change it without code.
- Use Apex when the logic needs loops over large sets of data, complex branching, or an API callout that Flow cannot express cleanly. Apex also suits cases where you want unit tests that cover edge cases.
- Use a prompt template when the result is generated language, not a data lookup. Examples are a reply draft or a case summary.
You can also combine them. A Flow can call Apex, and a prompt template can pull in data from a Flow. Keep the action the agent sees small and focused, even if the implementation behind it is layered.
What stays the same across targets
Three rules apply whichever target you choose:
- Write a clear label and description for the action, every input, and every output.
- Return only the values the agent needs to answer the user.
- Return a readable message when nothing is found or something fails.
The Apex example in this tutorial shows these rules in code. The GetOrderStatus class has a described request class, a described response class, and a message output for the not-found case. A Flow that does the same job would have an input variable orderNumber and output variables status and message, each with the same descriptions.
Common mistakes
- Choosing Apex by habit when a Flow would be easier to maintain.
- Using a prompt template for a data lookup. The model may invent values instead of reading them from a record.
- Building one large action that does several unrelated things. The agent then has a vague description to work from and cannot tell when to call it.
- Changing the target later without keeping the input and output names stable. Agents and topic instructions may refer to those names.
Exercise
A support team wants an action that takes a case number and returns the case status. The logic is one record query, and the admins maintain all automation. Which target fits best, and why?
Show answer
A Flow. The work is a simple record lookup, and admins can maintain a Flow without code. Apex would add code to maintain with no benefit. A prompt template generates text, so it is the wrong choice for returning a stored status value.
Exercise
Name one difference in how a Flow and an Apex invocable method expose inputs to the agent.
Show answer
A Flow exposes variables marked "Available for input". An Apex invocable method exposes fields on a request class marked with @InvocableVariable. In both cases the label and description are what the agent reads.