Sub topic 3
Unit Testing and Releasing an Agentforce Action
Assume you know basic Apex, SOQL, Salesforce test methods, and Agent Builder. Test the Apex action directly before testing the full agent. This separates Apex defects from agent configuration problems.
Unit testing the action
Call the invocable method with a list of request objects. Test successful results, empty results, limits, and multiple requests. Verify that each response matches the request at the same list position.
@IsTest
private class GetOpenCasesActionTest {
@IsTest
static void returnsOpenCaseSummary() {
Account accountRecord = new Account(Name = 'Test Account');
insert accountRecord;
insert new Case(
AccountId = accountRecord.Id,
Subject = 'Printer is down',
Status = 'New'
);
GetOpenCasesAction.Request requestRecord =
new GetOpenCasesAction.Request();
requestRecord.accountId = accountRecord.Id;
Test.startTest();
List<GetOpenCasesAction.Response> results =
GetOpenCasesAction.getOpenCases(
new List<GetOpenCasesAction.Request>{ requestRecord }
);
Test.stopTest();
Assert.areEqual(1, results.size());
Assert.areEqual(1, results[0].caseCount);
Assert.isTrue(results[0].summary.contains('Printer is down'));
}
}Add a test with an account that has no open cases. Assert the count and the exact user-facing message. Add a bulk test with several requests to expose accidental queries inside loops.
Adding and testing the action
In Agent Builder, open the topic that should use the action. Add an Apex action, select the deployed class and method, and review the generated input and output fields. Assign the action a clear instruction, such as “Use this action to list current open cases for the account.”
In the preview, use a request that supplies a real account. Confirm that the agent chooses the action, supplies the correct account ID, and explains the returned summary. Also test an account with no results and an invalid or missing input.
Deploying and designing safely
Deploy the Apex class, test class, and agent metadata together through your normal Salesforce deployment process. Run required tests in the target org, then verify the action again in Agent Builder.
Keep each action focused. Validate inputs, use with sharing, query in bulk, and return bounded results. Descriptions should state what the action does, when to use it, and what each input means. Avoid actions that combine unrelated updates or expose more data than the agent needs.
Exercise
What should you test after adding a passing Apex action to a topic?
Show answer
Test action selection, input mapping, successful output, empty results, and error handling in the agent preview.