Sub topic 9
Mapping action inputs and outputs
This chapter assumes you can build a prompt template in Prompt Builder and add it to an agent topic as an action. It also assumes you know what a topic and its instructions are. Here the focus is the data contract of that action: what the agent must supply when it calls the template, and what comes back.
Why the mapping matters
A prompt template action has two sides. The template declares inputs, which are the resources it needs, such as a Case record. The agent has to fill those inputs from the conversation or from earlier actions. When the template finishes, the generated text comes back as the action's output, and the agent decides what to do with it.
Most failures in agent-driven templates come from this contract, not from the prompt wording. The usual causes are an input the agent cannot fill, an input filled with the wrong value, or an output the agent rewrites or hides.
How template inputs become action inputs
When you add a template as an action, Agentforce reads the template's input resources and creates one action input for each. The merge fields in the template body show where each input is used:
- A record input such as
{!$Input:Case.Subject}produces an action input named after the object, hereInput:Case. The agent must supply a Case record. - A free text input produces a text action input. The agent can fill it directly from what the user wrote.
Each input has a name and a description. The reasoning engine uses these two things to decide what value goes where. Names like Input:Case are accurate but say little about how the agent should find a value. You can edit the description in the action configuration, and that is the main way to improve how reliably the input is filled.
| Input type | What the agent must provide | Typical source |
|---|---|---|
| Record (for example Case) | A record of that object type | Output of an earlier lookup action, or the record the user is viewing |
| Free text | A string | The user's message, or an earlier action's output |
Input settings in the action configuration
Agent Builder lets you control how each input is gathered. Exact labels differ between releases, so check your org. The settings you will meet are:
- Require input. The agent must have a value before it can run the action. If it has none, it asks the user. This prevents the template from running with an empty record.
- Collect data from user. The agent may ask the user a question to fill the input. Turn this off for inputs the user cannot know, such as an internal record ID. Those must come from another action.
- Input description. Text that tells the agent what a valid value looks like.
A record input is rarely something a user can type. A customer says "case 1234" and not a 15-character record ID. So a record input almost always needs a preceding action that turns what the user said into a record.
Chaining a lookup action to the template
The standard pattern has two actions in the same topic. The first resolves the user's words to a record. The second uses that record as the template input.
Here is an Apex invocable method that can serve as the lookup action. It is bulk-safe and returns a result for every request, even when no case matches.
public with sharing class CaseLookupAction {
public class Request {
@InvocableVariable(label='Case Number' required=true
description='The case number the user mentioned, for example 00001234.')
public String caseNumber;
}
public class Result {
@InvocableVariable(label='Case' description='The matching Case record.')
public Case matchedCase;
@InvocableVariable(label='Found' description='True when a case was found.')
public Boolean found;
}
@InvocableMethod(
label='Find Case by Number'
description='Finds a Case from its case number.')
public static List<Result> findCase(List<Request> requests) {
Set<String> numbers = new Set<String>();
for (Request r : requests) {
if (String.isNotBlank(r.caseNumber)) {
numbers.add(r.caseNumber.trim().leftPad(8, '0'));
}
}
Map<String, Case> byNumber = new Map<String, Case>();
for (Case c : [
SELECT Id, CaseNumber, Subject, Status, Description
FROM Case
WHERE CaseNumber IN :numbers
WITH USER_MODE
]) {
byNumber.put(c.CaseNumber, c);
}
List<Result> results = new List<Result>();
for (Request r : requests) {
Result res = new Result();
String key = String.isBlank(r.caseNumber)
? null : r.caseNumber.trim().leftPad(8, '0');
res.matchedCase = key == null ? null : byNumber.get(key);
res.found = res.matchedCase != null;
results.add(res);
}
return results;
}
}WITH USER_MODE makes the query respect the running user's field and object access, which matters because the agent runs on behalf of a user or a service identity.
Match types in the mapping step. The prompt template expects a Case record. If the lookup returns the record, you can map it directly. If your lookup returns only a text ID, the types do not match, and you must either return the record or look it up inside the template with a Flow resource. Check the type shown next to each input and output in the action configuration before you map them.
Mapping from earlier outputs, not from guesses
The agent can fill an input in three ways: from the user's message, from the output of an earlier action, or from a variable. Which source it chooses depends on your topic instructions and the input descriptions. Make the source explicit.
Topic: Case Hand-off
Instruction 1: If the user gives a case number, use the Find Case by Number action first.
Instruction 2: If Find Case by Number returns Found = false, tell the user the case was not found and ask for the number again. Do not run Summarize Case.
Instruction 3: When a case was found, run Summarize Case using the Case returned by Find Case by Number.Instruction 2 matters. Without it, the agent may call the template with an empty record, and the template will produce a vague answer built from missing fields.
Free text inputs as a simpler option
If a lookup action feels heavy, give the template a free text input, for example CaseNumber, and resolve the record inside the template with a Flow resource. The agent then only has to pass a string, which it can read straight from the user's message. The cost is that the lookup logic now sits in the template, so it is harder to reuse in other actions. Choose this when the template is the only consumer of the lookup.
| Approach | Advantage | Disadvantage |
|---|---|---|
| Separate lookup action plus record input | Lookup is reusable and testable on its own | Two actions to configure and describe in instructions |
| Free text input plus Flow resource | One action, simple for the agent | Lookup logic is tied to the template |
The output side
A prompt template action returns the generated text as a single output, usually named for the prompt response. Three things decide what happens to it.
- Whether the agent sees it. The agent uses an output to plan its next step only if the output is available to it. If you filter an output from the agent, it cannot use that value in later reasoning.
- Whether the user sees it directly. A setting such as "Show in conversation" displays the output to the user as returned. When it is off, the agent reads the output and then writes its own reply.
- What the instructions say to do with it. Instruction text like "return its output without rewriting the facts" pushes the agent to repeat the text instead of paraphrasing it.
For a summary that a support agent will read aloud, display the output directly and instruct the agent to pass it through. For output that feeds another step, such as a draft that a later action sends, keep it visible to the agent and hidden from the user until it is confirmed.
Diagnosing mapping problems
Use the action trace in Agent Builder's preview to see which inputs were sent and what came back. Common symptoms:
- The action never runs. A required input has no source. The agent keeps asking the user, or skips the action. Add a lookup action or relax the requirement.
- The user is asked for a record ID. "Collect data from user" is on for a record input. Turn it off and map the input from an earlier output.
- The summary is empty or generic. The template ran with a blank or wrong record. Add a not-found branch in the instructions and check the lookup's output in the trace.
- The wrong case is summarized. The agent reused an old record from earlier in the conversation. Tell the instructions to run the lookup again whenever the user gives a new case number.
- The reply differs from the template output. The output is not shown directly, and the agent paraphrased it.
Change one thing at a time and rerun the same test utterance, so you know which change fixed the problem.
Exercise
A user says "Summarize case 1234". The Summarize Case action has a required record input Input:Case, but the agent keeps asking the user for a case ID. List two changes that fix this.
Show answer
First, turn off "Collect data from user" for the Input:Case input, because users cannot supply a record. Second, add a lookup action such as Find Case by Number, and write a topic instruction that runs it first and passes its returned Case record to Summarize Case. Writing the input description to say the value comes from the lookup output also helps.
Exercise
The template output is accurate in the preview of Prompt Builder, but in the agent conversation the summary contains a promise of a refund that the template forbids. What most likely happened, and how do you prevent it?
Show answer
The agent did not show the template output directly and instead wrote its own reply, which added content. Show the output in the conversation as returned, and add a topic instruction to return the action's output without rewriting it.