Sub topic 4
Writing labels and descriptions the agent can use
This chapter assumes you can write an @InvocableMethod with request and response classes, as covered in the previous chapters. Here you will see how the text you attach to that code affects the agent's behavior.
Why the text matters
An Agentforce agent does not read your Apex. Its reasoning engine, which is a large language model, sees only the metadata: the action's label and description, and each input and output's label and description. It uses that text to decide three things:
- Whether this action fits what the user asked.
- Which value to put in each input.
- How to interpret each output when it replies.
Vague text leads to wrong choices. The action may not be called when it should be, or it may be called with a guessed value.
Where the text lives
Four places carry text the agent can use:
| Location | Attribute | Agent uses it to |
|---|---|---|
| `@InvocableMethod` | `label`, `description` | Choose the action |
| Input `@InvocableVariable` | `label`, `description` | Decide what value to pass |
| Output `@InvocableVariable` | `label`, `description` | Understand the result |
| Agent Builder registration | Action instructions | Add situational guidance |
A weak and a strong version
Compare two ways to describe the same input.
// Weak: the agent must guess what "id" means and where to get it
@InvocableVariable(label='Id')
public Id recordId;
// Strong: says which object, and how the value is obtained
@InvocableVariable(
label='Account Id'
description='The Salesforce Id of the account whose open cases should be listed'
required=true
)
public Id accountId;The strong version names the object and the purpose. Compare the action-level description too. "Gets cases" does not say which cases, for whom, or in what order. Lists the open support cases for an account, newest first does. It also tells the agent not to use this action for closed cases.
Guidelines for good text
- Start with a verb and say what the action does. "Lists", "Creates", "Updates".
- Name the object and the scope. "Open support cases for one account" is better than "cases".
- State defaults and limits in the input description. The example action says
Defaults to 5 if not provided, so the agent can leave the field empty. - Describe the format of values. If a date must be
YYYY-MM-DD, say so. - Describe outputs as you want them used.
One line per case with case number and subject, or a message if there are nonetells the agent what the string contains and that an empty result is a message, not an error. - Keep similar actions distinct. If you have "Get Open Cases" and "Get Case History", each description should say what the other does not do. Overlap makes the agent pick inconsistently.
- Do not put secrets or business rules you cannot afford to leak in descriptions. Treat them as visible to the model.
Common errors
- Reusing generic labels such as "Input" or "Result" across actions.
- Writing descriptions for developers ("Wrapper for SOQL results") instead of for the model.
- Making an input
required=truewhen the agent cannot know the value. It will then ask the user or invent one. - Changing a description after registering the action and expecting the agent to update. The registered action has its own copy of instructions, so re-check it in Agent Builder after a change.
Exercise
An action has the input @InvocableVariable(label='Date') public Date d; and the agent sometimes passes today's date when the user meant a due date. Rewrite the attribute so the agent can choose correctly.
Show answer
Give it a specific label and a description that names the meaning and format, for example @InvocableVariable(label='Due Date' description='The date the task is due, as a calendar date. Do not use today's date unless the user asks for it' required=true) public Date dueDate;. Renaming the field to dueDate also helps readers of the code, but the label and description are what the agent sees.