Sub topic 13
Testing and Refining Your Action Definitions
This chapter assumes you can already build an action in Agentforce Builder, that you know the difference between a topic and an action, and that you can read Apex or Flow variables. You have written the names, labels, descriptions, inputs, and outputs. Now you check whether the agent reads them the way you meant.
An action that works when you run it alone can still fail inside an agent. The agent never sees your code. It sees the metadata and decides from that text whether to call the action, what to put in each input, and how to describe the result. Testing an action definition therefore means testing two things: the logic, and the agent's reading of it.
Test in layers
Test from the inside out. Each layer isolates a different kind of fault, so a failure points to a likely cause.
| Layer | What you test | Typical fault found |
|---|---|---|
| Logic | The Apex method or Flow runs correctly with known inputs | Bad query, missing null check, wrong field |
| Definition | The agent can select the action and fill its inputs | Vague description, wrong data type, ambiguous input |
| Output | The agent reports the result accurately | Hidden or noisy output, unclear error message |
| Conversation | The action works across a multi-turn exchange | Topic instructions conflict with the action |
Do not skip the first layer. If the logic is wrong, no description change will fix it, and you will waste time rewording text that was never the problem.
Layer 1: test the logic on its own
For Apex, write a unit test that calls the invocable method with a list of requests. Cover the found case, the not-found case, and a bulk call with more than one request, because invocable methods receive lists.
@IsTest
private class GetOrderStatusTest {
@TestSetup
static void setup() {
Account acct = new Account(Name = 'Test Account');
insert acct;
Order o = new Order(
AccountId = acct.Id,
Status = 'Draft',
EffectiveDate = Date.today()
);
insert o;
}
@IsTest
static void returnsStatusForExistingOrder() {
Order o = [SELECT OrderNumber FROM Order LIMIT 1];
GetOrderStatus.Request req = new GetOrderStatus.Request();
req.orderNumber = o.OrderNumber;
Test.startTest();
List<GetOrderStatus.Response> results =
GetOrderStatus.getStatus(new List<GetOrderStatus.Request>{ req });
Test.stopTest();
System.assertEquals(1, results.size());
System.assertEquals('Draft', results[0].status);
}
@IsTest
static void returnsMessageWhenOrderIsMissing() {
GetOrderStatus.Request req = new GetOrderStatus.Request();
req.orderNumber = '99999999';
List<GetOrderStatus.Response> results =
GetOrderStatus.getStatus(new List<GetOrderStatus.Request>{ req });
System.assertEquals(1, results.size());
System.assertEquals(null, results[0].status);
System.assert(results[0].message.contains('No order was found'));
}
}The second test matters most for agent work. It confirms the not-found case returns a readable message and an empty status, which is what the output descriptions promise. If the description says "Empty if no order was found," a test should prove it.
For a Flow, run it in debug mode with sample values for each input variable. Check that every output variable is set on every path, including the path where a Get Records element finds nothing.
Layer 2: test whether the agent selects and fills the action
Once the logic passes, open the agent in Agentforce Builder and use the preview or test panel. Send realistic customer messages, not messages written by someone who knows the action's name.
Try at least these three kinds of messages:
- A direct request that should call the action: "Where is order 00000101?"
- A paraphrase that gives the same intent with no keywords from your label: "Has my purchase shipped yet?"
- A message that is missing a required input: "What's the status of my order?"
For each, look at the reasoning or execution trace the builder shows. Check three things:
- Did the agent choose the action you expected, or a different one?
- What value did it put in each input?
- Did it ask the user when a required value was missing, or did it invent one?
The third message tests the instruction in the input description: "Ask the customer if it was not given. Do not guess." If the agent fills the order number with a made-up value, the description is not strong enough, or the input is not marked required.
Layer 3: test the output
Read the final reply to the user and compare it to the raw output the action returned. The agent should state the result accurately and in plain language. Look for these problems:
- The agent repeats internal values such as record IDs that the user does not need.
- The agent says something the output does not support.
- The agent says "something went wrong" when your message described the exact problem.
- The agent shows a blank value instead of explaining that nothing was found.
Run the not-found path on purpose: "Where is order 99999999?" The reply should use your message, something like "No order was found with number 99999999," and should not claim a status.
Read the trace to find the cause
A bad result is a symptom. The trace tells you which part of the definition caused it. Use this map to move from symptom to fix.
| Symptom | Likely cause | Fix |
|---|---|---|
| Agent never calls the action | Action description does not match how users phrase the request | Rewrite the description around the user's intent and add a "Use when..." sentence |
| Agent calls the wrong action | Two descriptions overlap | Make each description state what it does and what it does not do |
| Agent invents an input value | Input is optional, or description lacks "do not guess" | Mark it required and state what to do when the value is missing |
| Agent asks for a value it already has | Description does not say it can come from the conversation or context | State where the value may come from |
| Input value in wrong format | Data type or example is unclear | Add an example in the description and use a more specific type |
| Reply includes irrelevant data | Output returns too many fields | Remove fields the agent does not need, or hide them from the agent |
| Reply misstates the result | Output label or description is ambiguous | Name the output after its meaning and describe the possible values |
| Reply is vague on failure | Error returned as an exception or empty value | Return a plain-language message in an output field |
Change one thing at a time
When a test fails, change one part of the definition, then run the same message again. If you reword the action description, an input description, and an output label together, and the result improves, you will not know which edit helped. Worse, you will not know which edit might hurt a different scenario.
A useful habit is to keep a short log:
- The message you sent.
- What the agent did.
- The one change you made.
- The result of the next run.
Language models do not always respond the same way to the same message. Run each important message more than once before you decide a fix works. A change that passes once and fails the next time has not fixed the problem.
Build a regression set
After you fix a case, keep it. A small set of messages you rerun after every change prevents a fix in one place from breaking another. Include:
- Direct requests for each action.
- Paraphrases that avoid your label's wording.
- Requests with a missing required input.
- Requests with an input in an unexpected format, such as an order number with spaces.
- Requests that return no results.
- Requests that belong to a different action or topic, so you confirm the action is not called when it should not be.
The last item is easy to forget. An action description that is too broad pulls in requests that belong elsewhere. Testing only the cases where the action should run will not reveal that.
Agentforce also offers testing tools that run a batch of utterances against an agent and compare the results to expected topics, actions, and responses. Check what is available in your org and release. If you use them, store your regression messages there so the whole set runs in one step. If you do not, a plain document of messages and expected behavior works.
Regression set: Get Order Status
1. "Where is order 00000101?" -> calls Get Order Status, order number 00000101
2. "Has my purchase shipped yet?" -> asks for the order number, does not call yet
3. "Status of order 99999999" -> calls action, replies that no order was found
4. "order # 00000101 please" -> calls action, order number 00000101
5. "I want to return my order" -> does not call Get Order StatusEdge cases to check
- Bulk and repeated calls. A customer may ask about two orders in one message. Confirm the agent calls the action twice, or that your design handles it, rather than merging both numbers into one input.
- Permissions. The Apex example uses
WITH USER_MODE, so results depend on the running user's access. Test with a user whose access matches a real agent user, not only as an administrator. An empty result caused by missing access looks the same as a missing record unless your message distinguishes them. - Changes to descriptions after activation. Editing metadata can change agent behavior. Rerun your regression set after any change to labels or descriptions, not only after code changes.
- Long outputs. If an output returns a large block of text, check that the agent summarizes it correctly and does not drop the important part.
- Similar actions in one topic. Adding a new action can change how the agent chooses among the existing ones. Rerun the set for the whole topic.
Know when to stop
Refinement has diminishing returns. Stop when your regression set passes consistently across several runs and the replies are accurate and clear. If one scenario still fails after several edits, consider whether the design is the problem. A single action that handles too many cases, an input that needs to be split in two, or an instruction in the topic that contradicts the action may all need a structural change instead of better wording.
Exercise
You test "Has my purchase shipped yet?" and the agent calls Get Order Status with the order number "unknown". The Apex unit tests all pass. What do you check and change first?
Show answer
The logic is not at fault, so look at the definition. Check that the orderNumber input is marked required and that its description says to ask the customer and not to guess. Strengthen the description if needed, change only that one thing, and send the same message again several times. Then add the message to your regression set.
Exercise
After you shorten the action description to fix one wrong-action case, a different test message stops calling the action at all. What practice would have caught this, and why?
Show answer
A regression set. Rerunning all saved messages after each change shows that a fix in one place broke another scenario. Rerunning only the failing message would not have shown it.
Exercise
Why should the regression set include a message that should not call your action?
Show answer
A description that is too broad can pull in requests meant for another action or topic. Only a message that should not trigger the action reveals this over-matching.