Sub topic 15
When to Move from Standard to Custom Actions
This chapter assumes you can add a standard action to a topic in Agentforce Builder. It also assumes you know what action inputs and outputs are, and that you have a working grasp of Flow, Apex, and permission sets. Here you learn how to decide whether a standard action is enough, which kind of custom action to build when it is not, and which mistakes to avoid.
Start with the standard action
Standard actions are prebuilt and maintained by Salesforce. You do not write or test their logic. Use one whenever it does the job, because a custom action is something your team must build, secure, test, and maintain.
Move to a custom action when one of these is true:
- No standard action performs the operation you need, such as counting open cases for an account.
- A standard action works, but only after you stack workarounds in topic instructions to force the behavior you want.
- The task needs fixed business rules that must run the same way every time.
- The task needs data or logic that the standard actions cannot reach, such as a calculation across related objects or a call to another system.
Keep the standard action when the only problem is wording. Unclear agent behavior is often an instruction problem, not an action problem. Fix the topic instructions and the action descriptions first. Then test again.
The three custom action types
You can add three kinds of custom actions to a topic. Each wraps a different kind of logic.
| Type | Best for | Strength | Cost |
|---|---|---|---|
| Flow | Multi-step record logic that an admin can maintain | Low-code, easy to change, familiar tooling | Complex branching gets hard to read |
| Apex | Aggregations, bulk logic, calls to code you already have | Full control and performance | Needs a developer and test classes |
| Prompt template | Generating text from record data | The model writes grounded text in a set format | Output is text, not a data change |
Flow actions
Choose a Flow action when the logic is mostly record work: look up several objects, apply a rule, update a field. An autolaunched flow exposes its input and output variables, and the agent fills the inputs the same way it fills a standard action's inputs. A flow is a good first choice because an admin can change it without a deployment.
Apex actions
Choose Apex when you need grouping, aggregation, or logic that Flow expresses badly. The method must be marked invocable, and each input and output field needs a label and a description. The agent reads those descriptions to decide how to fill inputs, so a vague description leads to wrong values.
Prompt template actions
Choose a prompt template action when you want the model to produce text from record data in a controlled format, such as a case recap that always has the same sections. The template defines the instructions and the merge fields. The result is text for the agent to relay, so do not use a template when the task must change data.
A worked example: a custom Apex action
Suppose the Account Support topic needs to answer "How many open cases does Acme have?" Query Records can return case rows, but counting through a list of rows is slow and error-prone in a conversation. A small Apex action returns the count directly.
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;
}
}Several details matter here:
with sharingmakes the query respect the sharing rules of the user the agent acts for. The user sees counts only for cases they can access.- The method takes and returns lists. Salesforce can call an invocable method with several requests at once, so the output list must match the input list in order and size.
- Accounts with no open cases get 0 because the code checks
containsKey. Without that check, the result would be null. - The query runs once for all requests, not once per request, which keeps it within governor limits.
The account ID is an input, so the agent must get it from an earlier step. Topic instructions handle this, as the next section shows.
Wire the custom action into the topic
After you create the Apex class, add the action in Agentforce Builder the same way you add a standard action. Pick the invocable method as the reference action type, then review the name, description, inputs, and outputs. Then update the topic instructions so the agent knows when to use it.
Topic: Account Support
Classification description:
Handle questions about a customer's account, such as looking up an account, summarizing it, or updating basic fields.
Instructions:
1. When the user names an account, first find the account record, then use the result for any later step.
2. Use the summary action when the user asks for an overview of an account.
3. Before updating a record, restate the field and the new value and ask the user to confirm.
4. If you cannot find exactly one matching record, ask the user to clarify instead of guessing.The instructions already tell the agent to find the account first. That is what supplies the account ID to the new action. You would add one more line, such as "Use Count Open Cases when the user asks how many open cases an account has." Keep each instruction specific to one action.
The diagram shows how the runtime flows when the agent uses both kinds of action in one conversation.
Decision criteria
Work through these questions in order.
- Does a standard action do the job with clear instructions? If yes, stop.
- Is the work mainly record reads and updates with simple rules? Build a Flow action.
- Do you need aggregation, complex logic, or reuse of existing code? Build an Apex action.
- Do you need generated text in a fixed format? Build a prompt template action.
- Does the task change data in a risky way? Add a confirmation step in the topic instructions, whichever type you choose.
When two types fit, prefer the one your team can maintain. A Flow that an admin can edit often beats Apex that only one developer understands.
Common errors and gotchas
- Weak descriptions. The agent chooses actions and fills inputs from names and descriptions. A label like "Run Logic" gives it nothing to work with. Describe what the action does and what each input means.
- Too many overlapping actions. If a standard action and a custom action do similar things, the agent may pick the wrong one. Remove or narrow the one you do not need in that topic.
- Running without sharing. An Apex class declared
without sharingcan return data the user should not see. Usewith sharingunless you have a reviewed reason not to. Also check field-level and object-level access where the logic needs it. - Returning too much. Every output goes back to the model. Return only the fields the agent needs, and avoid sensitive values that the conversation does not require.
- Skipping tests. Custom code needs unit tests for deployment. Beyond that, test the conversation in Agentforce Builder and check that the action ran with the inputs you expected.
- Changing an action after adding it. If you edit the inputs or outputs of a Flow or Apex method, refresh or re-add the action in the topic so the definition matches.
Exercises
Exercise
A topic uses Query Records to list a customer's open cases, and the agent often miscounts when the list is long. Which custom action type fits best, and why?
Show answer
An Apex action. It can count with an aggregate query and return one number, so the agent does not have to count rows itself. A Flow could work for a simple count, but Apex is better when you need grouping or bulk handling.
Exercise
Your Apex action is declared without sharing. A support rep who cannot see a restricted account asks the agent about it. What can go wrong, and how do you fix it?
Show answer
The action may return data the rep is not allowed to see, because it ignores their sharing rules. Declare the class with sharing so the query respects the rep's access.
Exercise
A standard action almost does what you need, but the agent sometimes skips it. Should you build a custom action right away?
Show answer
No. First check the topic instructions and the action description, since unclear wording is a common cause. Build a custom action only if the standard action cannot perform the operation even with clear instructions.