Sub topic 4
Writing Labels and Descriptions the Agent Can Use
This chapter covers how the text you attach to an invocable action changes the way an Agentforce agent chooses it and fills in its inputs. It assumes you can write an @InvocableMethod with request and response wrapper classes, as covered in the previous chapters. It also assumes you know what an agent topic is.
Why the text matters
An Apex developer usually writes names and comments for other developers. For an agent action, the reader is the agent's reasoning engine, which is a large language model. It cannot see your Apex logic. It sees only the action's metadata: its name, label, description, and the label and description of each input and output.
The engine uses that text for three decisions:
- Whether to call this action at all, given what the user asked.
- Which action to pick when several are available in the topic.
- What value to place in each input, and where to find that value in the conversation.
If the text is vague or missing, the engine guesses. A badly described action may be skipped when it is needed, called when it is not, or called with a wrong value. These are reasoning failures. They look like bugs in your code, but nothing in the Apex is wrong.
Where the text lives
The annotations carry the text. This is the action from the earlier chapter, reduced to the metadata parts:
@InvocableMethod(
label='Get Open Cases'
description='Lists the open support cases for an account, newest first'
category='Service'
)@InvocableVariable(
label='Account Id'
description='The Salesforce Id of the account whose open cases should be listed'
required=true
)
public Id accountId;The following table shows what each attribute does for the agent.
| Attribute | Where | Role for the agent |
|---|---|---|
| `label` | Method, variable | Short display name. Shown in Agent Builder and used as a signal for matching. |
| `description` | Method, variable | The main text the engine reads to decide when to use the action or how to fill a value. |
| `category` | Method | Groups the action in the Flow and Agent Builder pickers. It does not guide reasoning. |
| `required` | Variable | Tells the engine the input must be supplied before the call. |
Agent Builder also lets you override the action's instructions and the input and output descriptions after you register it. The next chapters cover registration. Write good annotation text first, because it is the default that travels with the class when you deploy it.
Describing the action itself
A good method description answers one question: when should the agent run this, and what does it return? Include these elements:
- The verb and the object: "Lists the open support cases for an account".
- The ordering or scope when it matters: "newest first", "only cases the user can see".
- A hint about when not to use it, if a similar action exists.
Compare these two descriptions for the same method.
| Weak | Stronger |
|---|---|
| `Gets cases` | `Lists the open support cases for one account, newest first. Use when the user asks what is open, pending, or unresolved for a customer. Does not include closed cases.` |
The weak version does not say which cases, for whom, or in what shape. If your topic also holds an action called "Get Case History", the engine has nothing to separate them. The stronger version states the boundary.
Labels
Keep a label short and specific, in the form verb plus object: "Get Open Cases", "Create Return Request". Avoid labels such as "Case Helper" or "Process". Labels should also be unique within a topic. Two actions both labeled "Get Cases" force the engine to guess from descriptions alone.
Describing inputs
Input descriptions have the most direct effect on correctness. The engine reads them to decide which fact from the conversation goes into which field. Good input descriptions state four things.
- What the value is, in business terms.
- The format, when there is one: "ISO date, such as 2026-12-31" or "18-character Salesforce Id".
- The default or allowed values, when they exist.
- What to do when the value is missing, if the engine should ask the user or leave it blank.
Here is a weak and a strong version of the maxCases input from the chapter example.
// Weak
@InvocableVariable(label='Max' description='Max')
public Integer maxCases;
// Stronger
@InvocableVariable(
label='Maximum Cases'
description='How many cases to include in the summary. Defaults to 5 if not provided'
)
public Integer maxCases;The stronger version tells the engine the field is optional and what happens when it is left empty. That statement must match your code. In the example, the method treats null or a value below 1 as 5. If the description promised a default of 10, the agent would describe behavior your method does not have.
Ids and names
A frequent failure is an input that asks for a Salesforce Id when the user only knows a name. The user says "Acme", and the engine has no Id to supply. There are two clean fixes:
- Keep the Id input, and describe where it comes from: "The Id of the account the user is currently viewing, or one returned by the Find Account action". The engine can then chain actions.
- Accept a name, and resolve it inside Apex. This moves matching logic into your code, so handle the zero-match and many-match cases explicitly.
Choose the first when a lookup action already exists. Choose the second when the user will always speak in names and ambiguity is rare.
Required versus optional
Mark an input required=true only when the method cannot work without it. A required input makes the engine ask the user before calling. That is right for accountId and wrong for a convenience setting like maxCases. Too many required inputs create long question chains. Too few lead to calls with empty values.
Describing outputs
Output text is easy to neglect, but the engine reads it to compose the reply. Describe what each field contains and how it is formatted.
@InvocableVariable(
label='Case Summary'
description='One line per case with case number and subject, or a message if there are none'
)
public String summary;The phrase "or a message if there are none" matters. It tells the engine that an empty result arrives as readable text, so it can relay that text rather than treat the empty value as a failure. The error handling chapter returns to this.
Prefer a small number of clearly described outputs over many fields. Each field adds text for the engine to read, and unused fields add noise.
Writing style for descriptions
The engine reads plain prose well. These habits help:
- Write full sentences or clear fragments. Avoid internal abbreviations and field API names unless the user would say them.
- State units and formats: "amount in US dollars", "number of days".
- Use the same terms the user and the topic instructions use. If the topic says "customer", do not write "account holder" in one action and "client" in another.
- Keep each description to one to three sentences. Long descriptions dilute the signal.
- Do not put instructions about tone or policy here. Those belong in the topic instructions.
Descriptions are not secret. Anyone with access to the class or to Agent Builder can read them, so never include credentials, internal URLs, or security assumptions.
How the pieces fit together
The agent first reads the topic and its action list, then matches the user's request against action descriptions. After selecting an action, it fills inputs from the conversation using input descriptions. After the call, it reads output descriptions to build its reply.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The agent never calls the action | The method description does not match how users phrase the request | Add the user's wording ("open", "pending", "unresolved") to the description |
| The agent calls the wrong action in a topic | Two descriptions overlap | Add a scope sentence to each, and say what the action does not do |
| The agent passes a name where an Id is expected | The input description does not say what format is required | State the format and where the value comes from |
| The agent keeps asking for a value the user already gave | The input is `required=true` with a vague description | Describe the value clearly, or make it optional |
| The reply misreports the result | The output description does not match what the method returns | Update the description to match the code |
| Description and behavior disagree | The code changed and the text did not | Review descriptions whenever you change defaults, formats, or filters |
A worked revision
Here is a poorly described action, followed by a revised version. The method returns the status of a return request.
public with sharing class GetReturnStatusAction {
public class Request {
@InvocableVariable(
label='Return Number'
description='The return request number the customer gives, for example RET-000123. Ask the customer if it is not known'
required=true
)
public String returnNumber;
}
public class Response {
@InvocableVariable(
label='Status'
description='Current status of the return, such as Submitted, Received, or Refunded, or a message if no return matches'
)
public String status;
}
@InvocableMethod(
label='Get Return Status'
description='Looks up the status of one return request by its return number. Use when a customer asks where their return is. Does not create or change returns'
category='Service'
)
public static List<Response> getReturnStatus(List<Request> requests) {
List<Response> responses = new List<Response>();
for (Request r : requests) {
Response res = new Response();
res.status = 'No return was found with number ' + r.returnNumber + '.';
responses.add(res);
}
return responses;
}
}This class is a stub and does no lookup yet. It shows the metadata pattern: a business meaning, a format example, a clear scope boundary, and an output that explains the empty case. The "Does not create or change returns" sentence also protects against the engine choosing this action for a create request.
Checking your text
Before you register an action, read its descriptions as if you knew nothing else about the system. For each action, ask:
- Could someone tell from the description alone when to use it?
- Could they tell it apart from the other actions in the topic?
- For each input, would they know what value to supply and in what format?
- For each output, would they know how to describe it to a customer?
Then confirm the text in the agent preview, which a later chapter covers. Reasoning behavior can change between model versions, so retest descriptions after platform releases.
Exercise
An agent has two actions in one topic. "Get Cases" has the description "Gets cases". "Get Open Cases" lists open cases for an account. Users ask "what is unresolved for Acme?" and the agent picks "Get Cases" with an account name in an Id input. Name two changes to the text that would improve this.
Show answer
First, rewrite the "Get Cases" description so it states its scope and how it differs from "Get Open Cases", for example by saying it returns closed cases only, or removing the action if it is redundant. Add the user's wording ("unresolved", "pending") to the "Get Open Cases" description so it matches the request. Second, rewrite the Id input description to state that it expects an 18-character Salesforce Id and where to get it, such as from a Find Account action, so the agent does not put a name in the field. Optionally, accept a name and resolve it in Apex.
Exercise
A description on an action says "Only use for premium customers." Does this restrict who can run the action? What should you do instead?
Show answer
No. Descriptions only guide the reasoning engine, and the agent can still call the action for anyone. Enforce the rule in Apex, for example by checking the account's tier and returning a clear message, and rely on with sharing and user-mode queries for data access. Keep the description sentence only as a hint.