Sub topic 9
Adding a custom action backed by Apex
This chapter assumes you have an agent created from a spec, a Salesforce DX project with an authorized org alias (my-dev-org), and basic Apex knowledge. A custom action lets an agent run your own logic when the built-in actions are not enough.
How an Apex action reaches the agent
An agent cannot call an arbitrary Apex class. It can call an invocable method, which is a static method marked @InvocableMethod. The agent reads the labels and descriptions on that method to decide when to call it and what to pass in. The descriptions you write are therefore part of the agent's instructions.
Write the invocable class
The class below takes a case number and returns its status. It uses an inner Request class for input and a Result class for output.
public with sharing class CaseStatusAction {
public class Request {
@InvocableVariable(label='Case Number' required=true)
public String caseNumber;
}
public class Result {
@InvocableVariable(label='Case Status')
public String status;
}
@InvocableMethod(label='Get Case Status' description='Returns the status of a case from its case number')
public static List<Result> getStatus(List<Request> requests) {
List<Result> results = new List<Result>();
for (Request req : requests) {
Result res = new Result();
List<Case> found = [
SELECT Status FROM Case WHERE CaseNumber = :req.caseNumber LIMIT 1
];
res.status = found.isEmpty() ? 'Not found' : found[0].Status;
results.add(res);
}
return results;
}
}Three details matter here:
with sharingmakes the query respect the running user's record access. The agent should not reveal cases that user cannot see.- The method takes and returns a
List. Salesforce may pass several requests in one call. - The
labelanddescriptionvalues are what the agent reads. "Returns the status of a case from its case number" tells it when the action applies.
The query inside the loop is fine for single requests. If you expect large batches, collect the case numbers first and query once.
Deploy the class
Deploy the class before you reference it. Apex deployment runs the usual compile checks.
sf project deploy start \
--source-dir force-app/main/default/classes/CaseStatusAction.cls \
--target-org my-dev-orgAttach the action to a topic
Once the class is in the org, add it as an action under the topic that should use it, for example a case_status topic. In Agent Builder, choose the Apex action type and select Get Case Status. Give the action instructions that say when to use it and what to ask the user for, such as "Ask for the case number if the user has not given one."
The action is stored as agent metadata, so you can retrieve it into your project and commit it. The earlier chapter on metadata covers that step. Your test spec must use the action name as it exists in the org. The spec in this tutorial uses GetCaseStatus.
Verify the behavior
Preview the agent first, then run tests.
sf agent preview --api-name Case_Helper --target-org my-dev-orgTry "What is the status of case 00001026?" and confirm the agent selects the topic, calls the action, and reports the status. Then try a case number that does not exist. The agent should relay "Not found" and not invent a status.
Common errors
| Symptom | Likely cause |
|---|---|
| Class does not appear as an action | The class is not deployed, or the method lacks `@InvocableMethod` |
| Agent never calls the action | The description is vague, or the topic instructions do not mention the need |
| Agent asks for the wrong input | The `@InvocableVariable` label is unclear |
| Test fails on expected action | The action name in the spec differs from the org |
Exercise
Why do the label and description on an invocable method matter more for an agent than for a Flow author who picks the action by hand?
Show answer
The agent chooses the action itself by reading those texts. A vague description can mean the action is never called, or is called at the wrong time. A person building a Flow can see the context and choose correctly regardless.
Exercise
You changed the Apex class and redeployed it, but sf agent test run still reports a missing action. What should you check first?
Show answer
Check that the action name in the test spec matches the action name configured on the topic in the org, and that the action is attached to the topic being tested.