Sub topic 13
Testing and Refining Your Action Definitions
This chapter assumes you have built an Agentforce action with a Flow or an Apex invocable method, that you know what a topic is, and that you can open Agentforce Builder. It covers how to test an action definition and how to change it based on what you see.
Why testing action definitions is different
A normal method either returns the right value or it doesn't. An action has a second layer. The agent has to pick the action, fill the inputs from the conversation, and explain the output. All three steps depend on the text you wrote in names, labels, and descriptions. An action can pass an Apex unit test and still fail in a conversation.
Test in two stages:
- Test the logic of the Flow or Apex on its own.
- Test how the agent uses the action in a conversation.
Stage 1: Test the logic first
Confirm that the target works before you involve the agent. If the logic is broken, you can't tell whether a bad answer came from the code or from the metadata.
For Apex, write a test that covers the found case and the not-found case. This matters because the not-found message is what the agent will read aloud.
@IsTest
private class GetOrderStatusTest {
@IsTest
static void returnsMessageWhenNoOrderMatches() {
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.assertEquals('No order was found with number 99999999.', results[0].message);
}
}For a Flow, run it in Flow debug mode with sample input values. Check the empty and error paths as well as the happy path.
Stage 2: Test with the agent
Open the agent in Agentforce Builder and use the conversation preview. Send realistic messages, then inspect how the agent reasoned. The preview shows which topic and action the agent chose, what values it passed in, and what the action returned.
Run these checks:
- Selection. Ask for the task in several wordings. Does the agent choose this action every time? Does it avoid the action when the request is unrelated?
- Input filling. Leave out a required value. The agent should ask for it instead of guessing. Give the value in a different format and see whether it still maps correctly.
- Output explanation. Does the answer use the returned status or message accurately? Does it add facts that the action never returned?
- Empty and error cases. Use an order number that doesn't exist. The agent should relay the message, not invent a status.
Read the failure, then pick the fix
Match the symptom to the part of the definition most likely to cause it.
| Symptom | Likely cause | Fix |
|---|---|---|
| Agent never calls the action | Action description doesn't match how users phrase the request | Rewrite the description to say when to use it |
| Agent calls it for the wrong request | Description is too broad | Add what the action does not cover |
| Agent invents an input value | Input description doesn't say to ask the user | Add "Ask the customer if it was not given. Do not guess." |
| Input arrives in the wrong format | Vague label or wrong data type | Add an example value and use a more fitting type |
| Agent states a wrong or extra fact | Output is unclear or too large | Return fewer, named fields and a plain message |
| Agent says "something went wrong" | Error or empty result not described | Return a clear message field |
Change one thing at a time, then rerun the same conversation. If you edit three descriptions at once, you can't tell which edit helped.
Keep a regression set
Save the conversations that exposed problems. After every change to a description, type, or topic instruction, replay them. A fix for one action can change how the agent chooses between actions, so rerun your other test conversations too.
Exercise
Your agent keeps asking for an order number even after the customer gives one in the message, or it makes one up when none is given. Name one place in the action definition you would check for each problem.
Show answer
If the agent doesn't pick up a given number, check the input label and description: they should name the value, show an example format, and the data type should fit. If the agent invents a number, check that the input description tells the agent to ask the customer and not to guess, and that the input is marked required.
Exercise
After you run a test conversation with a nonexistent order number, the agent says the order is "Activated". Which stage of testing finds the cause, and what do you look at?
Show answer
Stage 2, testing with the agent. Review the reasoning and action output in the preview to see what the action returned. If it returned an empty status and a not-found message, the agent is adding facts, so tighten the output descriptions. If the action returned "Activated", the problem is in the Apex or Flow logic from Stage 1.