Sub topic 12
Grounding Answers in Salesforce Data
This chapter assumes you know what topics, instructions, and actions are, and how the reasoning loop picks an action. It covers where an agent gets the facts it states, what happens when those facts are missing, and how to check that an answer came from real data.
What grounding means
A large language model (LLM) produces fluent text from patterns learned in training. It has no knowledge of your orders, cases, or accounts. If you ask it about order 00012345 and give it no data, it can still write a confident reply, and that reply will be invented.
Grounding means supplying the model with trusted data at the moment it writes the answer, and steering it to base the answer on that data. In Agentforce, grounding comes from three places:
- The results of actions the agent ran during the conversation.
- Text and record values merged into a prompt template.
- Passages retrieved from knowledge or other indexed content.
The model still writes the wording. Grounding controls which facts that wording is allowed to rest on.
Where grounding data comes from
| Source | How it reaches the agent | Typical use |
|---|---|---|
| Flow or Apex action | The action returns output values that go into the conversation | Looking up a specific record, such as an order status |
| Prompt template | Merge fields pull record values into the prompt text | Drafting a summary or reply about one record |
| Knowledge or a data library | Relevant passages are retrieved by search and added to the prompt (retrieval-augmented generation, or RAG) | Answering policy and how-to questions from articles and documents |
| Data Cloud | Unified or external data is exposed through retrieval or as inputs to actions | Questions that span systems beyond core CRM objects |
The first row matters most for this tutorial. An action is how an agent turns a vague request ("where is my order?") into a specific fact ("Status: Shipped"). Actions are both what the agent can do and what grounds the answer.
Which of these sources you use depends on the question. Questions about one record need an action that queries that record. Questions about policy need retrieval over documents. Mixing them up produces weak grounding. For example, asking RAG over articles for a live order status will return general text, not the customer's order.
How a grounded answer is built
Two points follow from the diagram. First, the data enters before the model drafts the reply, so a missing or wrong action result cannot be fixed afterward by better wording. Second, the topic instructions travel in the same prompt as the data, which is why they are the place to say how the data must be used.
Instructions that enforce grounding
Data alone does not stop the model from guessing. Instructions tell it what to do when the data is absent. The Order Support topic from earlier chapters includes this line:
Instructions:
- Ask for the order number if the customer has not given one.
- Use the order status action to look up the order before answering.
- If the order cannot be found, say so and do not guess.Each line does a different grounding job:
- The first line prevents the agent from calling the action with an invented input.
- The second line requires a lookup before any statement about status.
- The third line defines the behavior for an empty result, which is the case most likely to trigger a made-up answer.
Scope helps as well. The same topic says it does not process returns or refunds. A request outside that scope has no grounding data in this topic, so the agent should decline or hand off. It should not improvise.
An action that returns facts the agent can use
The Apex action below returns a status and also returns explicit values for the "no data" case. The descriptions are text the agent reads, so they should state what each value means.
public with sharing class OrderFactsAction {
public class Request {
@InvocableVariable(label='Order Number' description='The order number the customer provided' required=true)
public String orderNumber;
}
public class Response {
@InvocableVariable(label='Found' description='True if an order with this number exists and the user can see it')
public Boolean found;
@InvocableVariable(label='Status' description='Current status of the order. Empty when Found is false')
public String status;
@InvocableVariable(label='Last Updated' description='When the order record was last changed. Empty when Found is false')
public String lastUpdated;
}
@InvocableMethod(label='Get Order Facts' description='Looks up an order by order number and returns its status and last update time. Does not change the order')
public static List<Response> getFacts(List<Request> requests) {
Set<String> numbers = new Set<String>();
for (Request r : requests) {
if (r.orderNumber != null) {
numbers.add(r.orderNumber);
}
}
Map<String, Order> byNumber = new Map<String, Order>();
for (Order o : [
SELECT OrderNumber, Status, LastModifiedDate
FROM Order
WHERE OrderNumber IN :numbers
WITH USER_MODE
]) {
byNumber.put(o.OrderNumber, o);
}
List<Response> results = new List<Response>();
for (Request r : requests) {
Response res = new Response();
Order match = byNumber.get(r.orderNumber);
res.found = match != null;
if (match != null) {
res.status = match.Status;
res.lastUpdated = String.valueOf(match.LastModifiedDate);
}
results.add(res);
}
return results;
}
}Three design choices support grounding:
foundis a separate Boolean. The agent does not have to interpret a magic string such as "Not Found" and cannot confuse it with a real status.- Empty fields stay empty when nothing matched. The action never fills in a plausible default.
WITH USER_MODEmakes the query respect the running user's field-level security and sharing. The agent cannot ground an answer in data the user should not see.
Access control limits what can be grounded
Grounding data is read with the permissions of the user or service identity the agent runs as. If that identity cannot see an order, the action returns nothing, and the correct reply is "I couldn't find that order." For a customer-facing agent, that is the desired result.
Check this when an agent works for you in testing but fails for real users. Testing often runs as an administrator who sees everything. A customer-facing identity may see far less.
The Einstein Trust Layer adds protections around the model call, such as masking sensitive values before they reach the model and checking outputs. Those protections reduce data exposure and unsafe output. They do not verify that a statement is true. Accuracy depends on the data and instructions you provide.
Common failure modes
- No lookup happened. The agent answers from general model knowledge because no instruction or action description pushed it to call the action. Fix the instruction and the action description.
- Stale data. The action reads a record that is out of date, or retrieval reads an index that has not refreshed. The answer is grounded but wrong. Include timestamps in outputs, as
lastUpdateddoes above. - Ambiguous empty result. The action returns a blank or an error and the agent fills the gap. Return an explicit
foundflag and cover it in the instructions. - Wrong source for the question. Retrieval over articles is used for a record-specific question, or the reverse.
- Over-broad retrieval. Too many loosely related passages dilute the useful ones, and the model may cite the wrong one. Narrow what the library indexes.
- Permissions mismatch. The agent identity cannot read a field the action needs, so the field comes back empty.
Checking grounding in testing
When you test, look beyond whether the final text sounds right. Open the trace for the turn and check:
- Which action ran, and with which input values.
- What the action returned.
- Whether each claim in the reply appears in that output or in the retrieved passages.
Then run a negative case: give an order number that does not exist. A well-grounded agent says it could not find the order. An agent that invents a status has a gap in its instructions, its action output, or both.
Exercise
An Order Support agent tells a customer an order is "Shipped" when the order number does not exist. The action returns a blank status when no order matches. Name two changes that reduce the chance of this error, and say where each one goes.
Show answer
First, add an explicit output such as a Boolean "Found" to the action, and describe it on the output variable, so an empty result is unambiguous. Second, keep or add the topic instruction "If the order cannot be found, say so and do not guess", so the agent knows what to do with a not-found result. A third useful step is to test with a nonexistent order number and check in the trace that the reply matches the action output.
Exercise
An agent answers order questions correctly when tested by an administrator but says "I couldn't find that order" for real customers. The Apex action uses WITH USER_MODE. What is the most likely cause, and what should you check?
Show answer
The identity the agent runs as for customers likely lacks access to the Order records or to some fields the query selects. User mode enforces sharing and field-level security, so the query returns no rows. Check the permission sets and sharing for that identity, and retest the action as that user, not as an administrator.