Sub topic 7
Grounding with a Flow
This chapter assumes you can build an autolaunched flow in Flow Builder. It also assumes you know how merge fields insert record values into a template, which the earlier chapters covered.
When merge fields are not enough
Merge fields read values that already sit on the input record or on a parent reached through a relationship. Some grounding data needs work before it is useful:
- Child records, such as the last five orders for an account.
- Filtered or sorted data, such as open tasks only.
- Values you must calculate or format, such as total order value.
A Flow resource handles these cases. The flow runs when the template resolves, builds a block of text, and returns it. The template inserts that text where you place the resource.
How the pieces connect
Flow requirements
For a flow to appear as a resource, it must meet these conditions:
- It is an autolaunched flow (no screen) and it is active.
- It has an input variable of the record type, named
RelatedEntity, with "Available for input" selected. It receives the record the template is anchored to. - It has a text output variable named
Prompt, with "Available for output" selected.
The names RelatedEntity and Prompt matter. The Apex example in this tutorial uses the same names for the same reason. The template reads whatever text the flow places in Prompt.
Build the example flow
This flow returns recent orders for the account on a case.
- Create an autolaunched flow. Add the input variable
RelatedEntity(record, object Case). - Add a Get Records element on Order. Filter on
AccountIdequals{!RelatedEntity.AccountId}. Sort byEffectiveDatedescending. - Add a text variable named
Prompt, available for output. - Add a Loop over the Get Records result. Inside the loop, use an Assignment to append a line to
Prompt, for example- {!Current_Order.OrderNumber}, {!Current_Order.Status}, {!Current_Order.TotalAmount}. - After the loop, add a Decision. If no orders were found, assign
No recent orders.toPrompt. - Save the flow as
Get_Recent_Ordersand activate it.
Get Records cannot cap the number of rows when it returns a collection. To limit the list, add a counter variable and exit the loop after five iterations. A long, unbounded list can push the prompt past its size limit.
Use the flow in the template
In the template editor, choose Insert Resource, select Flow, and pick the flow. Prompt Builder inserts a merge field that names the flow and its output:
You are a support assistant. Suggest a next step for the case below.
Case subject: {!$Input:Case.Subject}
Customer account: {!$Input:Case.Account.Name}
Recent order history:
{!$Flow:Get_Recent_Orders.Prompt}
Use only the order history above when you refer to orders.
If it says there are no recent orders, do not mention orders.The $Input merge fields and the $Flow merge field resolve together. The model receives one plain text prompt, with the flow's text already in place.
Common errors
| Symptom | Likely cause |
|---|---|
| Flow is missing from the resource list | The flow is inactive, is not autolaunched, or has no variable marked available for input or output. |
| Resource inserts empty text | The `Prompt` variable is never assigned on some path, or Get Records found nothing and no Decision handles it. |
| Flow fails at preview | The input variable is not named `RelatedEntity`, or its object does not match the template's object. |
| Output is cut off or the prompt is too large | The loop appends too many records or long text fields. Cap the count and select only the fields you need. |
| Users see different results | The flow's run mode and the users' permissions differ. Check which context the flow runs in and which records each user can access. |
Design habits
- Always set
Prompton every path, including the "no data" path. An explicit fallback phrase lets the template instruct the model to skip that section. - Keep one flow focused on one block of data. Several small flows are easier to test and reuse than one large flow.
- Debug the flow on its own first. Run it in Flow Builder with a sample case and read the
Promptvalue before wiring it into a template.
Exercise
A template inserts {!$Flow:GetRecentOrders.Prompt}, but the flow does not appear in the Insert Resource list. Name three things to check.
Show answer
Check that the flow is active, that it is an autolaunched flow (no screens), and that it has a record input variable RelatedEntity marked available for input plus a text output variable Prompt marked available for output.
Exercise
Why should the flow assign a value such as "No recent orders." instead of leaving Prompt empty when Get Records finds nothing?
Show answer
An empty value leaves a blank section in the prompt, and the model may guess or invent orders. A fixed fallback text lets the template tell the model how to handle the missing data.