Sub topic 3
Writing a Custom Apex Action for Agentforce
Assume you know Salesforce objects, records, Setup, and basic Apex. You also need an agent open in Agentforce Builder. This chapter uses an Apex invocable method, which is an Apex method that Salesforce can expose to automation tools and agent actions.
Writing a custom Apex action
The action below receives account IDs, counts each account's open cases, and returns results in the same order as the requests.
public with sharing class GetOpenCaseCountAction {
public class Request {
@InvocableVariable(label='Account Id' required=true)
public Id accountId;
}
public class Response {
@InvocableVariable(label='Open Case Count')
public Integer openCaseCount;
}
@InvocableMethod(
label='Get Open Case Count'
description='Returns the number of open cases for an account'
)
public static List<Response> getOpenCaseCount(List<Request> requests) {
Set<Id> accountIds = new Set<Id>();
for (Request request : requests) {
accountIds.add(request.accountId);
}
Map<Id, Integer> counts = new Map<Id, Integer>();
for (AggregateResult result : [
SELECT AccountId accountId, COUNT(Id) total
FROM Case
WHERE IsClosed = false AND AccountId IN :accountIds
GROUP BY AccountId
]) {
counts.put(
(Id) result.get('accountId'),
(Integer) result.get('total')
);
}
List<Response> responses = new List<Response>();
for (Request request : requests) {
Response response = new Response();
response.openCaseCount = counts.containsKey(request.accountId)
? counts.get(request.accountId)
: 0;
responses.add(response);
}
return responses;
}
}Deploy the class, then refresh Agentforce Builder. Add Get Open Case Count as a custom action in the relevant topic. Give the action a clear description and explain when the agent should call it.
Using prompt templates as actions
A Prompt Builder template can also become an action. Use it when the agent needs controlled text generation, such as summarizing an account or drafting a response. Define its inputs, grounding records, and output format before adding it to a topic. Keep instructions specific so the agent does not use the template for unrelated requests.
Testing the agent in the conversation preview
Use the builder's conversation preview to test normal requests, missing account names, unrelated questions, and accounts with zero open cases. Confirm that the agent asks for missing information, calls the action, and reports the returned value instead of guessing.
Activating the agent and choosing where it runs
After testing, activate the agent from Agentforce Builder. Select only channels supported by your org and configured for the agent, such as an internal Salesforce experience or an available messaging channel. Verify the running user has access to the objects, fields, Apex class, and action.
Developer Edition limits and common setup problems
A Developer Edition org is for learning and prototyping. Features, data volume, users, channels, and some Agentforce or Data Cloud capabilities can differ from production. A missing action often means the class was not deployed, the method lacks the required annotations, or permissions are incomplete. If the preview cannot access records, check object permissions, field-level security, sharing, and the agent user.
Exercise
Why should the Apex method query all requested accounts in one SOQL query?
Show answer
Bulk processing avoids one query per request and helps the action stay within Salesforce governor limits.