Sub topic 5
Required inputs and asking the user
This chapter covers how to mark action inputs as required, how that setting changes the agent's behavior, and how to decide which values the agent should ask the user for. It assumes you know what an Agentforce agent, topic, and action are. It also assumes you can work with Flow variables or Apex classes, and that you have read the earlier chapters on names, labels, descriptions, and input design.
What "required" means to the agent
An action input is a value the agent must supply when it calls the action. Each input can be optional or required.
- A required input means the action cannot run without a value. The agent should not call the action until it has one.
- An optional input means the action can run without a value. The agent may fill it in if it has useful information.
The agent reasons about this from the action definition. If a required input has no value, the agent has two choices. It can find the value somewhere else, such as earlier in the conversation or in context it already holds. Or it can ask the user. The required setting tells the agent that an action call without that value is invalid. It does not tell the agent where the value comes from. Your descriptions and settings do that.
Where the setting lives
The same idea appears in several places, and they do not always carry over automatically. Check each layer.
| Layer | Setting | Effect |
|---|---|---|
| Apex invocable method | `required=true` on `@InvocableVariable` | Marks the input as mandatory in the action definition |
| Flow | Variable is **Available for input** | Exposes the variable as an input the agent can fill |
| Agentforce Builder, action input | **Require input** | Agent must have a value before it calls the action |
| Agentforce Builder, action input | **Collect data from user** | Agent asks the user for the value instead of inferring it |
The Builder settings belong to the action as it is used in your agent. The Apex and Flow settings belong to the underlying target. Treat the Builder as the place where you confirm the final behavior. Open each input after you add the action and check both settings.
Required versus collected
Two settings sound similar but answer different questions.
- Require input answers: can the action run without this value?
- Collect data from user answers: should the agent ask the user for this value?
Combining them gives four cases.
| Require input | Collect data from user | Typical use |
|---|---|---|
| On | On | The user must supply it, for example an order number |
| On | Off | The agent must have it, but gets it from context, such as the signed-in user's record |
| Off | On | Optional detail the agent may ask about, such as a preferred date |
| Off | Off | Optional value the agent fills only when it already knows it |
The second row matters most. Some values should never come from the user's typing. A customer ID, for example, should come from the authenticated session or a variable. If you leave Collect data from user on for such an input, the agent may ask the customer for an internal ID they do not know, or accept one they type in.
A decision flow
The following diagram shows how the agent treats a required input before it calls an action.
The last branch is a design error you should avoid. If an input is required, not collectable, and has no source in context, the action will never run. Every required input needs a path to a value.
Writing the input description so asking works
When the agent asks the user, it builds the question from your label and description. The description is where you control the quality of that exchange. The Apex example from earlier in this tutorial does this well:
@InvocableVariable(
label='Order Number'
description='The order number the customer provided, for example 00000101. Ask the customer if it was not given. Do not guess.'
required=true
)
public String orderNumber;Each part of that description does a job.
- "The order number the customer provided" says where the value comes from.
- "for example 00000101" shows the expected format, so the agent can recognize a valid value and prompt for the right shape.
- "Ask the customer if it was not given" states the collection behavior in plain language.
- "Do not guess" reduces the chance that the agent invents a value to satisfy the required flag.
That last point deserves attention. A required input creates pressure to produce a value. Without guidance, a model may fill the field with something plausible, such as a number from a different part of the conversation. A short instruction against guessing is cheap and effective.
For values that should not be asked of the user, say so:
Label: Account Id
Description: The Salesforce Id of the signed-in customer's account. Taken from the session. Never ask the customer for this value.Choosing what to require
Mark an input required only when the action truly cannot work without it. Each extra required input is another question the user may have to answer, and another way the action can stall.
Ask these questions for each input.
- Would the action's query, update, or calculation fail or return meaningless results without it? If so, require it.
- Can the action use a sensible default? If so, make it optional and apply the default in your code.
- Can the user easily provide it? If not, look for another source.
- Is the input only a filter that narrows results? Make it optional.
Here is a complete Apex example with one required and one optional input. The optional input has its default applied in code, so the agent does not need to supply it.
public with sharing class GetRecentCases {
public class Request {
@InvocableVariable(
label='Account Id'
description='The Id of the customer account. Taken from the session. Never ask the customer for this value.'
required=true
)
public Id accountId;
@InvocableVariable(
label='Maximum Cases'
description='How many recent cases to return. Optional. Use only if the customer asks for a specific number. Defaults to 5.'
)
public Integer maxCases;
}
public class Response {
@InvocableVariable(
label='Case Summary'
description='A short plain-language list of recent cases with subject and status. Empty if the account has no cases.'
)
public String summary;
}
@InvocableMethod(
label='Get Recent Cases'
description='Lists the most recent support cases for one account. Use when the customer asks about their recent or open support requests.'
)
public static List<Response> getCases(List<Request> requests) {
List<Response> results = new List<Response>();
for (Request r : requests) {
Integer limitCount = (r.maxCases == null || r.maxCases < 1) ? 5 : Math.min(r.maxCases, 20);
List<Case> cases = [
SELECT Subject, Status
FROM Case
WHERE AccountId = :r.accountId
WITH USER_MODE
ORDER BY CreatedDate DESC
LIMIT :limitCount
];
Response res = new Response();
if (cases.isEmpty()) {
res.summary = 'No cases were found for this account.';
} else {
List<String> lines = new List<String>();
for (Case c : cases) {
lines.add(c.Subject + ' (' + c.Status + ')');
}
res.summary = String.join(lines, '; ');
}
results.add(res);
}
return results;
}
}Two details in the code are worth noting. The method treats a missing maxCases as 5, so the agent never needs to ask about it. It also caps large values at 20, because the agent may pass whatever number the user states.
Flow: the same ideas with variables
In a Flow action, an input is a variable with Available for input selected. Flow has no required flag on the variable in the same sense as Apex, so you express the requirement in two places: the variable's description, and the Require input setting in Agentforce Builder. If your Flow cannot run without a value, say so in the description and handle the empty case inside the Flow, for example with a decision element that returns a clear message. Do not rely on the Builder setting alone.
Variable data types affect asking. A Text variable accepts whatever the user says. A picklist-like set of allowed values is not enforced by the type, so list them in the description, for example "One of: Standard, Express, Overnight."
Common mistakes
- Requiring too much. Five required inputs turn one request into an interview. Merge, default, or derive values where you can.
- Requiring an input with no source. The action never runs, and the agent may loop or give up.
- Collecting identifiers from users. Internal Ids should come from context or earlier actions.
- Leaving the description vague. "The id" gives the agent nothing to ask with.
- Forgetting the Builder settings. An Apex
required=truedoes not guarantee that Collect data from user is set the way you want. - Not telling the agent not to guess. Required fields invite fabricated values.
Link to topic instructions
Topic instructions can reinforce how the agent collects values, for example "Always confirm the order number with the customer before looking it up." Keep the per-input rules in the input description and keep the topic instructions for the sequence of the conversation. A later chapter covers aligning the two.
Exercise
An action takes Customer Id (a Salesforce Id, required) and Delivery Date (optional). The customer is signed in, and the Id is available from the session. Which settings should you use for each input, and what should the Customer Id description say?
Show answer
For Customer Id, turn Require input on and Collect data from user off, because the agent must have the value but should get it from the session, not from the customer. The description should state the source and forbid asking, for example: "The Id of the signed-in customer. Taken from the session. Never ask the customer for this value." For Delivery Date, turn Require input off. Turn Collect data from user on only if the agent should ask when the customer is discussing a delivery. The description should say it is optional, give the expected date format, and say to use it only if the customer mentions a date.
Exercise
A required input named Id has the description "The id". In testing, the agent calls the action with a case number it found earlier in the conversation. What is wrong, and how would you fix it?
Show answer
The label and description do not say which Id is meant, where it comes from, or what to do when it is missing, so the agent guesses from nearby text. Rename the label to something specific, such as "Order Number". Rewrite the description to state the source, give an example format, and add "Ask the customer if it was not given. Do not guess." Then retest with a request that omits the value.