Sub topic 7
Grounding with a Flow
Merge fields cover data that sits on the input record or on a parent reachable by relationship. They cannot filter, sort, loop, combine objects, or format values. A Flow resource covers those cases. The flow does the data work and returns one block of text, and the template inserts that text where you place it.
This chapter assumes you have read the earlier chapters on template inputs and merge fields. It also assumes you can build an autolaunched flow in Flow Builder, with Get Records, Decision, Loop, and Assignment elements. It builds one flow, wires it into a template, and covers the failure modes.
What a Flow resource is
A Flow resource is an autolaunched flow that Prompt Builder calls while it resolves a template. The flow receives the template's input record and returns a text value. Prompt Builder substitutes that text into the template, then sends the finished prompt to the model.
The contract between the template and the flow is fixed:
| Part | Requirement |
|---|---|
| Flow type | Autolaunched flow, with no screens and no pause elements. |
| Input variable | A record variable named `RelatedEntity`, set to "Available for input". Its object matches the template's input object. |
| Output variable | A text variable named `Prompt`, set to "Available for output". |
| Status | Active. The template uses the active version. |
If the names or availability settings differ, the flow does not appear as a usable resource, or it returns nothing. Most "my flow isn't listed" problems come from one of these four settings.
In the template body, the reference has the form {!$Flow:FlowApiName.Prompt}. The part after $Flow: is the flow's API name, not its label. The part after the dot is the output variable.
Recent order history:
{!$Flow:Get_Recent_Orders.Prompt}How resolution works
When a user runs the template, Prompt Builder resolves every merge field and resource before it calls the model. For a Flow resource it passes the input record as RelatedEntity, runs the flow, and reads Prompt.
Two consequences follow from this order:
- The model never sees the flow, only its text. Whatever the flow returns is the whole of the grounding, so the quality of the text matters as much as the query behind it.
- Resolution waits for the flow. A slow or failing flow slows or breaks every use of the template.
Build the flow: recent orders for a case
The goal is a template line that lists the customer's most recent orders. The case holds the account, and the orders hang off the account. A merge field cannot traverse from Case to a list of child orders, so a flow is the right tool.
Step 1: Create the variables
Create the flow as an autolaunched flow with no trigger. Then create:
RelatedEntity: Record variable, object Case, single record, Available for input.Prompt: Text variable, Available for output.OrderCount: Number variable, 0 decimal places, default value 0. This one is internal.
Step 2: Get the orders
Add a Get Records element on Order. Filter on AccountId equals {!RelatedEntity.AccountId}. Sort by EffectiveDate descending. Store all records in a new Order collection variable, Orders.
Get Records has no "limit to N rows" setting. You can store the first record or all records, and you cannot ask for the first five. There are two common ways to cap the list:
- Add a date filter, for example
EffectiveDateis greater than or equal to a{!$Flow.CurrentDate}offset formula such as 90 days back. - Loop over the sorted collection and exit the loop once a counter reaches your cap.
This flow uses the counter, because "the last five orders" is easier to explain to a user than "the last 90 days". The date filter is still worth adding if accounts can have thousands of orders, because Get Records loads every match into memory.
Step 3: Handle the empty case
Add a Decision after Get Records. If Orders is null, assign Prompt the text No orders on file for this account. and end the flow. Otherwise continue to the loop.
An explicit sentence is better than an empty string. With an empty value, the model sees a heading followed by nothing and may invent content to fill the gap.
Step 4: Build the text in a loop
Create a Text Template resource named OrderLine with this content:
- Order {!Order_Loop.OrderNumber}, {!Order_Loop.Status}, total {!Order_Loop.TotalAmount}, effective {!Order_Loop.EffectiveDate}Then loop over Orders with the loop element named Order_Loop:
- Add an Assignment inside the loop that sets
PrompttoPromptplus{!OrderLine}plus a newline, and adds 1 toOrderCount. - Add a Decision after the assignment. If
OrderCountis greater than or equal to 5, leave the loop. Otherwise continue.
Flow has no join function for text collections, so appending to a text variable inside a loop is the standard way to build a list. A newline in a flow formula is BR(). In a Text Template resource you can press Enter to include one.
Step 5: Add a fault path
Connect a fault path from Get Records to an Assignment that sets Prompt to Order history is temporarily unavailable. and ends the flow.
This is a trade-off. Without a fault path, a query failure stops prompt resolution and the user sees an error. With one, the template still runs, but the model works without order data and the user may not notice. Use the fallback when the order data is optional context. Leave it out when a response without orders would mislead the agent.
Step 6: Debug and activate
Use Flow Builder's debug run with a real Case Id. Check the Prompt value in the debug output. Then save and activate the flow. Debugging the flow first isolates logic errors from template errors.
Wire the flow into the template
In Prompt Builder, open the template and place the cursor where the data belongs. Use the resource insertion control, choose Flow, and pick Get_Recent_Orders. Prompt Builder writes the {!$Flow:Get_Recent_Orders.Prompt} reference for you. A full template looks like this:
You are a support assistant. Draft a reply to the customer about the case below.
Case subject: {!$Input:Case.Subject}
Case description: {!$Input:Case.Description}
Customer account: {!$Input:Case.Account.Name}
Recent order history:
{!$Flow:Get_Recent_Orders.Prompt}
Refer to an order only if it appears in the list above. If the list says there are no orders, do not mention orders. Do not guess order details.This template mixes both mechanisms. Merge fields handle the simple lookups, and the flow handles the list. Use merge fields wherever they work, because they are cheaper to maintain.
The last paragraph of the prompt matters. The flow supplies data, but the instructions decide how the model treats it. The rule "only if it appears in the list" turns the grounding into a boundary. Without it, the model may add plausible orders of its own.
What a good flow output looks like
Plain text with one fact per line works best. A resolved prompt for a case might contain:
Recent order history:
- Order 00001042, Activated, total 1299.00, effective 2026-11-18
- Order 00001017, Draft, total 249.50, effective 2026-11-02
- Order 00000988, Activated, total 87.00, effective 2026-09-30Some formatting rules help the model:
- Label every value, as in "total 1299.00", so a number is never ambiguous.
- Put dates in an unambiguous format. A Date value written into text by Flow follows the running user's locale or the flow's default, so test with users in different locales if your org has them.
- Do not return record Ids unless the instructions need them. They use up space and add nothing for the reader.
- Keep each line short and consistent, so the model can scan the list.
Gotchas
- Input object mismatch. If the template's input object is Case and the flow's
RelatedEntityis Account, the flow is not offered. Create one flow per input object, or build a flow that expects the right object and uses the Ids it needs. - Field availability. The record passed in as
RelatedEntityis a snapshot. If you need a field that might not be on it, re-query the record with Get Records using its Id. Check this in the preview rather than assuming. - Run mode and security. Autolaunched flows can run in user mode or system mode, depending on the flow's settings. In system mode, the flow can read records the current user could not see, and that data then flows into the prompt and the response. Pick the mode deliberately. The security chapter returns to this.
- Versioning. Activating a new flow version changes what every template using it receives, with no change to the template itself. Re-run your previews after activating.
- Governor limits. The flow runs inside normal Flow limits, such as the query row and element execution limits. A Get Records on a big object with no filter is the usual cause of trouble.
- Output size. The returned text becomes part of the prompt and counts toward the model's input limit. Cap the number of rows and the length of text fields you include. Long descriptions in a loop can fill the prompt faster than expected.
When the flow is the wrong choice
A flow is a good fit when the logic is a query plus formatting. It gets awkward when you need:
- Grouping or aggregation beyond what Get Records and a loop do cleanly.
- Complex string handling, such as truncating text at word boundaries.
- Calls that must be bulk-safe across many records at once.
Those cases point to Apex, covered in the next chapter. Flow can also call an invocable Apex action as an element, so a flow can keep its simple structure and delegate one hard step.
Checklist before you ship
- The flow is active and its API name matches the template reference.
RelatedEntityandPromptexist, with the right availability settings.- The empty case returns a clear sentence.
- The row count is capped.
- The run mode matches your security intent.
- You have previewed the template with at least three records, including an empty one.
Exercise
A template uses {!$Flow:GetRecentOrders.Prompt}. The flow works in Flow Builder debug, but it does not appear in Prompt Builder's resource list. Name two settings to check. Then explain why a Get Records element alone cannot return "the five newest orders" and give one way to get that result.
Show answer
Check that the flow is an active autolaunched flow, that it has a record variable named RelatedEntity set to Available for input with the same object as the template's input, and that it has a text variable named Prompt set to Available for output. Any two of these are correct. Get Records can store only the first record or all matching records, and it has no row limit setting. To get five, sort by EffectiveDate descending and loop over the collection with a counter that exits at 5. A date filter that restricts the query is an alternative cap.
Exercise
Your flow returns an empty string when an account has no orders, and the model sometimes invents order numbers in its replies. What two changes fix this?
Show answer
First, make the flow return an explicit sentence such as "No orders on file for this account." instead of an empty string. Second, add an instruction to the template that tells the model to mention only orders that appear in the list and not to guess details.