Sub topic 6
Choosing Data Types and Structure
This chapter assumes you know what an Agentforce action is and can read basic Apex or Flow variable definitions. It covers how the data type of each input and output affects how reliably the agent fills in values and reads results.
Why types matter to the agent
The agent fills inputs by reading the user's words and producing a value that fits the declared type. A narrow type gives the agent less room to be wrong. A wide type, such as free text for everything, moves validation onto the model and onto your code.
The agent also reads output types. A clearly typed output with a good description is easier to explain than a large, loosely shaped one.
Common types and when to use them
| Type | Good for | Watch for |
|---|---|---|
| Text (String) | Names, order numbers, free-form questions | Order numbers with leading zeros stay safe as text. A number type would drop them. |
| Number, Integer | Quantities, counts | Do not use for identifiers. |
| Currency, Decimal | Amounts | State the currency in the description. |
| Boolean | Yes/no choices | The agent may guess when the user is vague. Say what true and false mean. |
| Date, DateTime | Deadlines, delivery dates | State the expected format and time zone behavior. |
| Picklist-like text | Fixed sets of values | List the allowed values in the description. |
| Record or Id | Specific Salesforce records | The agent usually cannot invent a valid Id. |
Prefer simple, flat inputs
Each input should be one value the user can plausibly state. Compare two designs for an action that reschedules a delivery:
- Weak: one input called
Detailsthat holds "order number, new date, reason" in a single text value. The agent must format it, and your code must parse it. - Stronger: three inputs,
Order Number(text),New Delivery Date(date), andReason(text, optional).
Separate inputs let the agent collect each value on its own and ask only for the one that is missing. They also let you mark each input required or optional, as covered in the previous chapter.
Structured and collection data
Both Flow and Apex allow structured values, such as an Apex class used as a variable type or a list of records. Use them with care.
- Inputs: Avoid complex objects and lists. The agent has to build them from conversation, and a malformed structure fails the whole call.
- Outputs: Structure is more acceptable here, because your code builds it. Keep it small. A list of the five most recent orders is easier to summarize than every field on every order.
- Apex invocable methods receive and return lists, because Salesforce can bulk-call them. In the chapter's order status example,
List<Request>andList<Response>are the bulk wrapper. EachRequeststill holds simple fields.
The Apex class below shows a flat input and a flat, typed output with a date and a boolean:
public with sharing class GetDeliveryInfo {
public class Request {
@InvocableVariable(
label='Order Number'
description='The order number the customer provided, for example 00000101. Ask the customer if it was not given.'
required=true
)
public String orderNumber;
}
public class Response {
@InvocableVariable(
label='Estimated Delivery Date'
description='The estimated delivery date. Empty if the order has not shipped.'
)
public Date deliveryDate;
@InvocableVariable(
label='Has Shipped'
description='True if the order has shipped. False if it is still being prepared.'
)
public Boolean hasShipped;
}
@InvocableMethod(
label='Get Delivery Info'
description='Returns the shipping state and estimated delivery date for one order.'
)
public static List<Response> getInfo(List<Request> requests) {
List<Response> results = new List<Response>();
for (Request r : requests) {
Response res = new Response();
res.hasShipped = false;
results.add(res);
}
return results;
}
}This sketch returns a default value so it compiles. In a real action, you would query the order and fill in each field.
How types flow through an action
Common errors
- Using a number type for an identifier, which strips leading zeros.
- Leaving date format unstated, so the agent sends a value your code cannot parse.
- Passing a whole record or list as an input.
- Returning every field on a record when the answer needs two.
Exercise
An action takes a customer's postal code as input. Which data type should the input use, and why? Name one thing to put in its description.
Show answer
Text. Postal codes are identifiers, and a number type would drop leading zeros, as in some US codes. The description could give an example value and say to ask the customer if it was not provided.
Exercise
An action has one text input named "Details" that expects "order number, new date, reason". How would you redesign it?
Show answer
Split it into separate inputs: Order Number (text, required), New Delivery Date (date, required), and Reason (text, optional). The agent can then ask only for what is missing, and your code no longer parses a combined string.