Sub topic 4
Grounding prompts with merge fields and resources
This chapter explains how a prompt template gets real data from Salesforce. It covers merge fields, the other resource types, and the way the template type and input name affect the syntax. You should already know objects, records, fields and relationships. You should also know how to open Prompt Builder in Setup, and what a prompt template contains (instructions plus resources).
What grounding means
A large language model (LLM) only knows the text it is given, plus what it learned in training. If you ask it to "write a follow-up email to the contact" and give it nothing else, it will invent a name, a company and a product. Grounding means adding trusted, current data to the prompt so the model writes from facts instead of guesses.
In Prompt Builder, grounding happens when the prompt is built. The template holds placeholders. Each time the template runs, Salesforce replaces the placeholders with values from a specific record or another source. The model never sees the placeholder. It sees the finished text.
The flow looks like this:
Grounding reduces invented details, but it does not remove them. The model can still ignore data or add claims, so the instructions in the template still matter.
Merge fields
A merge field is a placeholder that points to a field on a record. In Prompt Builder it is written as {!$Input:Object.Field}. The part after $Input: is the input, followed by a dot and the field API name.
Here is the Sales Email style prompt from this tutorial's examples:
Write a short, friendly follow-up email to {!$Input:Contact.FirstName} {!$Input:Contact.LastName}, who works at {!$Input:Contact.Account.Name}.
Mention that we met recently and offer to schedule a call.
Keep it under 120 words and do not invent product details.Three things are worth noticing:
{!$Input:Contact.FirstName}reads a field on the Contact the template is based on.{!$Input:Contact.Account.Name}follows a lookup relationship from Contact to its Account and reads the account's name. This is how you reach parent records.- The last line of the prompt is an instruction, not data. Merge fields supply facts. Plain sentences supply behavior.
You do not have to type the syntax by hand. In the template editor, an Insert Resource control lists the fields and resources available for the template. Picking from the list avoids typos in API names and shows only what the template can reach.
Where the input comes from
The record behind $Input depends on the template type:
| Template type | What `$Input` refers to | How the name is set |
|---|---|---|
| Sales Email | A Contact or Lead record, and optionally a related record | Fixed by the type |
| Field Generation | A record of the object whose field will be filled | Fixed by the object you choose |
| Record Summary | A record of the object being summarized | Fixed by the object you choose |
| Flex | One or more inputs you define, such as a record of any object or free text | You name it, and the name appears in the syntax |
For the first three types, Salesforce decides the input for you. Choosing the object is what ties the template to a record, and the Insert Resource list is built from that object.
Flex templates differ because you define the inputs. If you add an input of type Record, pick the object Case, and call it Case, the merge field is {!$Input:Case.Subject}. If you named the input something else, the part after $Input: would be that name. The summary prompt below works as a Record Summary template on Case, or as a Flex template with a Case input:
Summarize this case for a support manager in three bullet points: the problem, what has been tried, and the next step.
Subject: {!$Input:Case.Subject}
Description: {!$Input:Case.Description}Check the input name in the template's input list before you copy a prompt between templates. A mismatched name is a common cause of an empty resolved value.
Handling missing data
A merge field resolves to whatever is in the record. If the field is blank, the model receives a blank. Left alone, it may fill the gap with something plausible. The Field Generation example addresses this in the prompt itself:
Using the information below, write a one-sentence description of this account for a sales rep.
Account name: {!$Input:Account.Name}
Industry: {!$Input:Account.Industry}
Annual revenue: {!$Input:Account.AnnualRevenue}
If a value is empty, leave it out instead of guessing.The final line tells the model what to do when Industry or AnnualRevenue has no value. Without it, the model may write an industry that sounds right for the company name.
Other patterns that help:
- Label each value (
Industry: ...). A bare value next to other bare values is easy to misread. - Say what to do with missing data, as above.
- Give the model a way out, such as "If there is not enough information, say so."
- Keep the fields few and relevant. Every extra field costs prompt length and can distract the model.
Resources beyond merge fields
A merge field reads one field. A resource is the broader term for a source of data you can insert into a template. Depending on your org and template type, the Insert Resource list can include:
- Record fields: the merge fields described above, including fields reached through lookups.
- Related lists: child records such as an account's contacts, cases or opportunities, inserted as a block of text. Use these when one record is not enough context.
- Flows: a Flow runs when the prompt is built and returns text. This suits logic you cannot write as a field, such as summing recent activity or choosing among records.
- Apex: an invocable Apex method returns text for the prompt, for data and logic that Flow does not handle well.
- Data Cloud data: data from Data Cloud, where your org has it enabled, for information that lives outside core Salesforce objects.
- Other prompt templates: the output of one template can be embedded in another, so you can reuse a summary inside an email.
The available resources vary with the template type, the edition and what is set up in the org. If a resource is not in the list, it is usually a setup or permission gap, not a syntax problem.
Choosing between a field, a related list and a Flow
| Need | Best resource | Why |
|---|---|---|
| One or two values from the main record | Merge field | Simplest, no extra setup |
| Facts about a parent, such as the account | Merge field through a relationship | No extra resource required |
| A list of child records | Related list | Brings several records in one block |
| Computed or filtered data | Flow | Logic runs before the prompt is built |
| Logic that needs code | Apex | Full control over the returned text |
Start with the simplest option that supplies the facts. Reach for a Flow or Apex only when a field or related list cannot express what you need.
Permissions and data access
Grounding data is resolved with respect to what the running user may see. If a user cannot read a field because of field-level security, that field does not resolve for them. The same prompt can therefore produce different grounding for different users.
Two consequences follow:
- Test the template as a user with realistic permissions, not only as an administrator. An administrator sees everything, so problems with restricted fields only show up for ordinary users.
- Sensitive data that you merge into a prompt is sent to the model provider, subject to the Einstein Trust Layer, which a later chapter covers. Include only fields the task needs.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| A merge field appears as literal text in the output | Typo in the input name or API name | Reinsert it from the Insert Resource list |
| A value is blank in the preview | The field is empty on that record, or the user cannot read it | Preview with a record that has data, and check field-level security |
| The model invents a fact | The data was missing and the prompt did not say what to do | Add an instruction for empty values |
| A pasted prompt stops working in Flex | The input name differs from the one in the prompt | Rename the input or edit the merge fields |
| A related list is huge | Too many child records are included | Narrow the list or summarize it with a Flow |
A short checklist
- Identify the record or input the template is based on.
- List the facts the model needs, and insert each one from the resource list.
- Label every value so the model can tell them apart.
- Add an instruction for empty values.
- Preview with complete, sparse and restricted records.
Exercise
A teammate copies the case summary prompt into a new Flex template. The input there is named "SupportCase", but the prompt still uses {!$Input:Case.Subject}. The preview shows the Subject line empty or unresolved. What is the cause, and what two changes would fix it? Then add one line to the prompt that stops the model from guessing when the Description field is empty.
Show answer
The merge fields refer to an input called Case, but the Flex template's input is named SupportCase, so the placeholders do not match any input. Fix it by either renaming the input to Case or editing the merge fields to {!$Input:SupportCase.Subject} and {!$Input:SupportCase.Description}, ideally by reinserting them from the Insert Resource list. To handle empty data, add a line such as "If the description is empty, say that no description was provided instead of guessing."