Sub topic 3
Adding a Custom Action Backed by Apex
Assume you know basic Salesforce DX project structure, Apex, Git, and command-line use. An invocable Apex method is an Apex method that Flow or Agentforce can call. An action exposes that method with a label and description that help the agent select it.
Add the Apex action
Create an Apex class such as CaseStatusAction.cls:
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 request : requests) {
Result result = new Result();
List<Case> cases = [
SELECT Status
FROM Case
WHERE CaseNumber = :request.caseNumber
LIMIT 1
];
result.status = cases.isEmpty() ? 'Not found' : cases[0].Status;
results.add(result);
}
return results;
}
}The method accepts and returns lists because invocable methods must support bulk execution. Descriptions matter: they provide selection guidance to the agent.
Write and run agent tests
A test specification checks topic selection, action selection, and the expected outcome:
name: Case Helper Tests
description: Checks case status behavior
subjectType: AGENT
subjectName: Case_Helper
testCases:
- utterance: What is the status of case 00001026?
expectedTopic: case_status
expectedActions:
- GetCaseStatus
expectedOutcome: The agent tells the user the current status.Deploy and run it:
sf agent test create --spec specs/caseAgentTest.yaml \
--api-name Case_Helper_Tests --target-org my-dev-org
sf agent test run --api-name Case_Helper_Tests --wait 10 \
--result-format human --target-org my-dev-orgTopic and action names must match the metadata in the target org.
Activation and CI checks
Activate only after preview and tests pass:
sf agent activate --api-name Case_Helper --target-org my-dev-org
sf agent deactivate --api-name Case_Helper --target-org my-dev-orgIn CI, authenticate a temporary org, deploy metadata, run the test specification, and fail the job when the CLI returns a nonzero status.
Agent Script and bundles
Agent Script provides a code-oriented way to author an agent bundle, including topics, instructions, and actions. Use it when repeatable source files and code review matter. Use generated specs when a guided starting point is faster. Keep the resulting metadata in Git and deploy it consistently.
Troubleshooting
- Unknown action: Check that the Apex class is deployed and its label matches the test spec.
- Wrong topic: Review topic descriptions and expected topic names.
- Authorization errors: Reauthorize the target org and verify permissions.
- No test results: Increase
--waitor inspect the org's asynchronous job status.
Exercise
Why does the invocable method return a list instead of one status value?
Show answer
Agentforce invokes actions in bulk, so the method must accept and return lists.