Sub topic 7
Grounding prompts in CRM data
This chapter covers dynamic grounding: how Agentforce adds CRM data to a prompt before the prompt goes to a large language model (LLM). It assumes you know Salesforce objects, fields, and user permissions, and that you know what an LLM and a prompt are. The previous chapters covered secure data retrieval and how permissions apply to it. This chapter covers what happens to the retrieved data next.
What grounding is
An LLM knows nothing about your customers, your orders, or your open cases. It only knows its training data and whatever text sits in the prompt. Grounding is the practice of adding relevant, current business data to a prompt so the model answers from facts instead of guesses.
Dynamic grounding means the data is pulled in at the moment of the request. The prompt is not written once with fixed text. It is a template with placeholders, and each placeholder is filled with live data for that request.
Grounding serves two goals:
- Relevance. The answer refers to the actual case, account, or contact.
- Accuracy. The model is told what the facts are, which reduces invented details (hallucinations). Grounding does not remove hallucinations completely.
Where grounding sits in the flow
Grounding happens after secure data retrieval and before masking and prompt defense.
The order matters. Retrieval decides which records and fields the running user may see. Grounding inserts only that data into the prompt. Masking then protects sensitive values in the assembled prompt before it leaves Salesforce.
Prompt templates and merge fields
In Salesforce, grounding is usually set up in a prompt template, which you build in Prompt Builder. A template mixes fixed instructions with merge fields. A merge field is a reference to a record field, written in double braces. When the template runs, Salesforce replaces each merge field with the value for the record in context.
Here is a simplified template for case summaries:
You are a support assistant. Summarize the case below in three sentences.
Contact: {!$Input:Case.Contact.Name}
Subject: {!$Input:Case.Subject}
Description: {!$Input:Case.Description}
Status: {!$Input:Case.Status}At run time the placeholders become real values. The model receives something like this:
You are a support assistant. Summarize the case below in three sentences.
Contact: Maria Lopez
Subject: Order arrived damaged
Description: Order 55501 arrived with a cracked screen.
Status: OpenThe exact merge field syntax depends on the template type and resource type, so check the Prompt Builder documentation for your org. The idea stays the same: fixed instructions, plus data inserted at run time.
Sources of grounding data
Grounding data can come from several places. Which ones you use depends on the template and the agent.
| Source | What it adds | Typical use |
|---|---|---|
| Record fields | Values from the record in context | Summarize a case, draft an email to a contact |
| Related lists and related records | Data from child or parent records | Include recent activities or order lines |
| Flow | Data assembled by a flow | Compute or look up values that are not simple fields |
| Apex | Data returned by custom code | Complex logic, external calls |
| Data Cloud | Unified customer data and retrieved knowledge | Cross-system profiles, knowledge articles |
Each source has its own access rules. A merge field on a record respects field-level security. A flow or Apex class may run in a different context, which is covered below.
Retrieval-augmented generation
Some grounding needs unstructured content such as knowledge articles, manuals, or policy documents. You cannot merge a whole document library into a prompt. Instead, the system uses retrieval-augmented generation (RAG):
- Documents are indexed ahead of time, often in Data Cloud.
- At request time, the system searches the index for passages relevant to the question.
- The best-matching passages are added to the prompt as grounding.
The model then answers from those passages. RAG keeps prompts short, because only the relevant passages are included, and it keeps answers current, because the index can be refreshed without changing the model.
How permissions shape the grounded prompt
Grounding is only as safe as the retrieval that feeds it. For record fields, the prompt is built with the running user's access. If a service agent cannot see a field because of field-level security, that field's value does not appear in the prompt. If the user cannot access a record because of sharing rules, the record is not used.
This produces a practical result. Two users who run the same template on the same record can send different prompts, and get different answers, because their permissions differ.
Take care with agents and automation. Some actions run in system context, or run with a different identity from the user who asks the question. Apex declared without sharing, or a flow set to run in system mode, can return data the user could not read directly. Grounding built on those sources can put that data into the prompt. The Trust Layer cannot tell that a value was returned by custom logic that bypassed sharing. Review the context in which each flow and Apex source runs.
Common errors and gotchas
- Blank or missing values. A merge field that resolves to nothing may be empty because the field is blank, or because the user lacks access. Test the template as a user with the same profile and permission sets as your real users, not as an admin.
- Too much data. Every grounded field consumes prompt space, and models have a limit on prompt size. Long text fields, large related lists, and many retrieved passages can crowd out the instructions or be cut off. Include only what the task needs.
- Too little data. A vague prompt with few fields leads to generic answers. If answers miss obvious facts, check which fields the template includes.
- Stale or poor-quality data. Grounding repeats whatever the record says. Duplicate contacts, outdated statuses, or free-text notes with errors produce confident but wrong answers. Data quality is a customer responsibility.
- Sensitive data in free text. Masking can detect certain patterns, such as names and email addresses, but it works on what it can detect. Long text fields such as case descriptions and notes can hold sensitive details in forms that detection misses. Think about what you ground before relying on masking to clean up afterward.
- Grounding is not authorization for actions. Grounding controls what the model can see. It does not control what an agent action is allowed to do. Action permissions are a separate design decision.
Grounding and masking together
Once the template is filled, the assembled prompt is passed to the next stage. Here is the example from the plan, before and after masking:
Original grounded prompt (inside Salesforce):
Summarize the case for Maria Lopez ([email protected]).
Her order 55501 arrived damaged.
Prompt after masking (sent to the LLM):
Summarize the case for PERSON_0 (EMAIL_0).
Her order 55501 arrived damaged.Notice that the order number was not masked. The placeholders and the set of detected values depend on configuration. Grounding decides what goes into the prompt. Masking then handles the sensitive values that are in it. Neither step replaces the other.
Design guidelines
- Ground in the smallest set of fields that lets the model do the task.
- Prefer record fields and RAG over broad flows or Apex, because their access rules are easier to reason about.
- Test templates as users with realistic permissions.
- Audit flows and Apex that feed grounding for sharing and field-level security behavior.
- Clean up the underlying data. The model cannot correct what the record gets wrong.
Exercise
A support manager runs a case summary template and gets a complete summary. An agent runs the same template on the same case and the summary leaves out the case description. Give the most likely cause, and name the first thing to check.
Show answer
The agent most likely lacks field-level security access to the Description field, so retrieval did not return it and grounding left it out of the prompt. First, check the field-level security for Case.Description on the agent's profile and permission sets. Then test the template as a user with the agent's permissions.
Exercise
A template uses an Apex class marked without sharing to return related account notes. Why is this a concern for grounding, and what would you review?
Show answer
Apex running without sharing can return records the running user could not otherwise read, and grounding places those values in the prompt. The Trust Layer cannot see that the sharing rules were bypassed. Review whether the class needs to bypass sharing, and change it to enforce sharing and field-level security where possible.