Sub topic 15
When to Move from Standard to Custom Actions
This chapter assumes you can add standard actions to a topic in Agentforce Builder, and that you know what Flow, Apex, and prompt templates are at a basic level. Standard actions cover common work such as finding, summarizing, and updating records, answering from Knowledge, and drafting emails. Sooner or later a task will not fit them. This chapter helps you decide when to extend or replace a standard action.
Signs a standard action is not enough
Try the standard action first. Move to a custom action when you see one of these problems:
- The task needs logic the standard action does not offer, such as a calculation, a multi-object rule, or a call to an external system.
- The agent needs data shaped in a specific way, for example a count of open cases, and Query Records cannot return it reliably.
- A business process must always run the same steps in the same order. Topic instructions only guide the agent, so they do not guarantee that.
- You need a specific output format, such as a fixed-style customer letter.
Do not move to custom because instructions are unclear. If a standard action runs at the wrong time, fix the topic instructions and the action descriptions first.
Choosing the custom action type
| Need | Best fit |
|---|---|
| Declarative logic, record changes, approvals, or callouts you can build without code | Flow |
| Complex queries, aggregation, or logic that needs code | Apex |
| Generated text with a controlled prompt and merge fields | Prompt template |
Start with Flow when you can. Admins can maintain it without code. Choose Apex when Flow becomes hard to read or cannot express the logic.
Example: an Apex action
Counting open cases for an account needs aggregation. An invocable Apex method works as a custom action. The @InvocableVariable and @InvocableMethod descriptions matter, because the agent reads them to decide when to call the action and how to fill its inputs.
public with sharing class OpenCaseCounter {
public class Request {
@InvocableVariable(label='Account Id' description='The account whose open cases are counted' required=true)
public Id accountId;
}
public class Result {
@InvocableVariable(label='Open Case Count' description='Number of open cases for the account')
public Integer openCases;
}
@InvocableMethod(label='Count Open Cases' description='Returns the number of open cases for an account')
public static List<Result> countOpenCases(List<Request> requests) {
Set<Id> accountIds = new Set<Id>();
for (Request r : requests) {
accountIds.add(r.accountId);
}
Map<Id, Integer> counts = new Map<Id, Integer>();
for (AggregateResult ar : [
SELECT AccountId acct, COUNT(Id) total
FROM Case
WHERE IsClosed = false AND AccountId IN :accountIds
GROUP BY AccountId
]) {
counts.put((Id) ar.get('acct'), (Integer) ar.get('total'));
}
List<Result> results = new List<Result>();
for (Request r : requests) {
Result res = new Result();
res.openCases = counts.containsKey(r.accountId) ? counts.get(r.accountId) : 0;
results.add(res);
}
return results;
}
}The class uses with sharing, so the query respects the running user's sharing rules. The method also handles a list of requests in one query, which keeps it within governor limits.
Common errors
- Vague descriptions. The agent skips the action or passes the wrong input. Write descriptions that say what the action does and what each input means.
- Ignoring access. Custom code can bypass the protections standard actions give you. Use
with sharingand check field-level security where it applies. - Replacing too much. Keep standard actions for lookups and summaries. Add a custom action only for the gap, then reference it in the topic instructions.
Exercise
A topic must return the number of open cases for a named account. Query Records returns unreliable totals. Which custom action type fits best, and what should you set on the sharing keyword?
Show answer
An Apex invocable action fits, because the task needs aggregation (COUNT with GROUP BY). Declare the class with with sharing so the query enforces the running user's sharing rules. Add clear descriptions to the inputs, outputs, and method so the agent knows when to call it.