Sub topic 5
Defining input and output variables
This chapter assumes you can build a basic autolaunched flow and know what a Flow variable is. You should also know that an Agentforce agent is driven by a language model. The earlier chapters in this tutorial covered how agents, topics, and actions relate, and how an autolaunched flow becomes the foundation of an action. Here you decide what goes into the flow and what comes out.
Why variables are the action's interface
An autolaunched flow has no screen. Nothing else can pass it values except the caller. When an agent calls the flow as an action, the flow's variables form the whole contract between the two:
- Input variables are the values the agent must supply before the flow runs.
- Output variables are the values the agent receives after the flow finishes.
- Every other variable is internal. The agent never sees it.
The agent cannot read your flow's elements or formulas. It sees only the action name, the instructions, and the inputs and outputs, and its language model uses these to decide when to call the action and what to ask the user. So the variable names, data types, and descriptions you define here shape the agent's behavior directly.
Making a variable an input or output
In Flow Builder you create a variable as a resource. Its settings include two checkboxes under the heading for Input/Output:
- Available for input lets a caller set the variable when the flow starts.
- Available for output lets the caller read the variable when the flow ends.
In the flow's metadata these settings are the isInput and isOutput elements. The following example shows one input and one output for a case lookup. Note that each variable also has a description.
<variables>
<name>caseNumber</name>
<dataType>String</dataType>
<isCollection>false</isCollection>
<isInput>true</isInput>
<isOutput>false</isOutput>
<description>The Case Number of the case to look up, for example 00001026.</description>
</variables>
<variables>
<name>caseStatus</name>
<dataType>String</dataType>
<isCollection>false</isCollection>
<isInput>false</isInput>
<isOutput>true</isOutput>
<description>The current Status of the case, or a short message if no case was found.</description>
</variables>A variable can be both an input and an output. That is occasionally useful, but it usually makes the action harder for the agent to reason about. Prefer separate variables, so each one has a single purpose.
Choosing names
The variable's API name is part of what the agent reads. Use names that state what the value is, in the vocabulary a customer or agent would use.
| Weak name | Better name | Reason |
|---|---|---|
| `var1` | `caseNumber` | The name alone says what to collect. |
| `input_text` | `customerEmail` | A specific noun beats a generic container. |
| `result` | `caseStatus` | The output says which fact it holds. |
| `flag` | `isEligibleForRefund` | A Boolean should read as a yes/no question. |
Keep names stable. If you rename a variable after you have registered the flow as an action, the action in Agent Builder may still refer to the old interface. After any change to inputs or outputs, open the action in Agent Builder and confirm that it shows the variables you expect. If it does not, add the flow again.
Choosing data types
The data type controls what the agent is allowed to pass in, and what shape the answer takes. Common choices:
- Text for names, identifiers, emails, and any value that is not used in arithmetic.
- Number for quantities and amounts.
- Boolean for true or false answers.
- Date and Date/Time for dates. The agent must produce a value in a format the flow can accept.
- Record variables for a whole Salesforce record.
- Collections of any of these when the action handles several values.
Check that each type you choose is supported for flow actions in your org's release. The supported list has changed between releases.
Some type decisions are easy to get wrong:
- Identifiers that look like numbers. The case number in the example is
00001026. Stored as a Number, it becomes1026and the Get Records lookup fails. Store it as Text. - Picklist values. If an input must be one of a few values, such as
Low,Medium, orHigh, use Text and say which values are valid in the description. The agent will not infer valid values from the field's setup. - Dates. Ask for a Date only when you need one. A request like "last week" must be turned into a concrete date by the agent, and that conversion can be wrong.
- Whole records as inputs. A Record input forces the agent to build a structured record. This is hard to do reliably in conversation. Prefer one input per field, or an identifier that the flow uses to fetch the record itself.
Designing inputs
Ask for the smallest set of values that makes the action work. Each additional input is another question the agent may have to ask, and another chance for a wrong value.
Follow these rules:
- Require only what the flow cannot do without. If the flow can look up a record by a single identifier, do not also ask for the customer's name.
- Prefer identifiers the user knows. A case number or order number works in conversation. A 15- or 18-character record ID does not, unless an earlier action already returned it.
- Do not set defaults that hide missing values. If you give
caseNumbera default of an empty string, the flow runs with no case number. The agent never learns that it should have asked. Instead, add a Decision element at the start that checks for a blank input and returns a clear message, as described in the error handling chapter. - Validate inside the flow. The agent might pass a value in the wrong format, so treat every input as untrusted text. Trim it, check it, and fail clearly when it does not match.
Designing outputs
The agent reads outputs and then writes a reply to the customer. Make this easy:
- Return facts, not internal codes. An output such as
Escalatedis more useful than3. If the underlying value is a code, translate it with a formula or an assignment. - Return a message when nothing is found. If Get Records finds no case, the output should say so. An empty output gives the agent nothing to report, and it may invent an answer.
- Keep outputs few and focused. Return what the agent needs to answer the question. Extra outputs add noise and can expose data that the customer should not see.
- Assign every output on every path. If a path ends without setting an output variable, the agent receives an empty or default value. Walk through each branch of the flow and confirm that each one sets the outputs.
An output is not private just because the agent might not repeat it. Treat any output as information the agent can show to the user, and decide what to include on that basis. The security chapter covers this in more detail.
Connecting the variables to the action
When you register the flow in Agent Builder, the inputs and outputs appear as the action's inputs and outputs. You then give the action instructions, and each input a setting for how its value should be gathered. The following sketch shows these values for the case lookup. The chapter on action instructions covers each setting in detail.
Action name: Get Case Status
Flow: Get_Case_Status (autolaunched flow)
Action instructions: Use this action when the customer asks about the status of an existing case and has given a case number.
Input caseNumber: Collect from the user. Ask for it if it was not provided.
Output caseStatus: Tell the customer the status in plain language.Notice how little of this is new information. The input name, caseNumber, and the output name, caseStatus, already carry most of the meaning. The instructions only tell the agent when to use the action. If your variable names need a paragraph of instructions to be understood, rename the variables first.
Common errors
- Missing checkbox. The variable exists in the flow but is not marked available for input or output, so it does not appear on the action. Open the variable and check both settings.
- Wrong type for identifiers. A Number type strips leading zeros, as described above.
- Output never assigned. A Decision branch skips the Assignment element that sets the output, and the agent replies with nothing useful.
- Vague names and descriptions. Names such as
text1force the agent to guess. It may put the wrong value in the wrong input. - Over-wide interfaces. A flow with eight inputs makes the agent ask eight questions. Split it into smaller actions, or have the flow derive values itself.
- Stale interface. You changed a variable after registration and the action still shows the old one. Check Agent Builder after every interface change.
Exercise
A flow looks up an order by order number such as 000457. You made orderNumber a Number variable, and the lookup returns nothing for valid orders. What is wrong, and how do you fix it? Also name one thing the flow should do when no order is found.
Show answer
A Number variable drops the leading zeros, so 000457 becomes 457 and no order matches. Change orderNumber to Text so the value is kept exactly as the customer gave it. When no order is found, the flow should set the output variable, for example orderStatus, to a short message such as "No order was found with that number", so the agent has something accurate to tell the customer.