Sub topic 10
When a Rule Needs a Flow or Apex Action Instead of an Instruction
This chapter assumes you can write topic instructions, reference actions in them, and use Agentforce Builder to preview a conversation. It covers how to decide whether a rule belongs in an instruction or in deterministic logic.
Why instructions are not guarantees
Topic instructions guide a large language model (LLM). The model reads them and usually follows them, but it interprets language and can vary between conversations. That is fine for tone, ordering, and phrasing. It is a poor fit for rules where one wrong answer costs money or breaks policy.
A Flow or Apex action runs the same way every time. It takes inputs, runs your logic, and returns outputs. The agent then reports the result. So the split is simple: let the instruction decide how to talk, and let code decide what is true.
Signs a rule needs logic
Move a rule out of the instructions when any of these apply:
- It involves arithmetic. Discounts, refund amounts, totals, and date math should be computed, not estimated.
- It depends on exact thresholds. "Refunds over $100 need approval" must behave the same at $100.00 and $100.01.
- It must read or change records. Looking up an order or updating a case needs an action.
- It has compliance or financial consequences. Eligibility, credit decisions, and entitlements need an audit trail.
- It has many conditions. A rule with five branches is hard for a model to apply consistently.
- You have already reworded it several times and testing still shows misses.
Example: a refund rule
Here is a rule written as an instruction:
If the order is more than 30 days old, or the item is a gift card,
or the total is over $100 and the customer has had two refunds this year,
do not offer a refund.The rule has three conditions that combine. The model has to compare dates, check item type, read a total, and count past refunds. Any step can go wrong.
Move the decision into an action. The Apex class below is a sketch of an invocable method. It receives plain values and returns a clear answer.
public with sharing class RefundEligibility {
public class Request {
@InvocableVariable(required=true)
public Date orderDate;
@InvocableVariable(required=true)
public Decimal orderTotal;
@InvocableVariable(required=true)
public Boolean isGiftCard;
@InvocableVariable(required=true)
public Integer refundsThisYear;
}
public class Result {
@InvocableVariable
public Boolean eligible;
@InvocableVariable
public String reason;
}
@InvocableMethod(label='Check Refund Eligibility')
public static List<Result> check(List<Request> requests) {
List<Result> results = new List<Result>();
for (Request r : requests) {
Result res = new Result();
res.eligible = true;
res.reason = 'Eligible';
if (r.orderDate < Date.today().addDays(-30)) {
res.eligible = false;
res.reason = 'Order is older than 30 days';
} else if (r.isGiftCard) {
res.eligible = false;
res.reason = 'Gift cards cannot be refunded';
} else if (r.orderTotal > 100 && r.refundsThisYear >= 2) {
res.eligible = false;
res.reason = 'Refund limit reached for orders over $100';
}
results.add(res);
}
return results;
}
}The instruction becomes short and testable. The action name here is hypothetical, so use the exact name of the action assigned to your topic.
1. Collect the order number from the customer if they have not given one.
2. Use the Check Refund Eligibility action with the order details.
3. If eligible is true, offer to start the refund.
4. If eligible is false, tell the customer the reason returned by the action. Do not offer a refund.The instruction no longer contains the policy. It only says when to call the action and what to do with each result.
Choosing between Flow and Apex
Both work as actions. Pick the one your team can maintain.
| Need | Better fit |
|---|---|
| Simple record lookup or update, built by admins | Flow |
| Branching you can draw as a few decisions | Flow |
| Complex calculation or heavy data handling | Apex |
| Logic that needs unit tests and version control | Apex |
| Reuse across several systems | Apex |
How the decision flows
Common mistakes
- Duplicating the rule in both places. If the instruction restates "30 days" and the action also checks it, changing one leaves the other stale. Keep the policy in the action only.
- Ignoring the output. Return a clear field such as
reason, so the agent can explain the result without inventing one. - Letting the agent guess inputs. Make inputs required so the action cannot run on missing data.
- Moving everything to code. Tone, order of questions, and handoff wording still belong in instructions.
Exercise
A topic instruction says "Give a 10% discount to customers with more than 5 orders, and 15% if they have more than 10 orders." Testing shows the agent sometimes gives the wrong percentage. What should you do, and what should the instruction say afterward?
Show answer
Move the discount calculation into a Flow or Apex action that takes the customer's order count and returns the discount percentage. The instruction should only say to call the action with the customer's record and to tell the customer the percentage the action returns. The thresholds live in the action, so they are applied the same way every time.
Exercise
Name two kinds of rules that should stay in instructions rather than move into a Flow or Apex action.
Show answer
Rules about tone and phrasing, such as keeping replies to one or two sentences, and rules about conversation order, such as asking for the order number before looking anything up. These shape how the agent talks and do not need exact computation.