Sub topic 9
Choosing between merge fields, Flow, and Apex
This chapter assumes you can write a prompt template with merge fields, have built a simple autolaunched Flow, and can read Apex. Here you will learn how to pick the right grounding resource for a given need.
The decision in one sentence
Use the simplest resource that supplies the data. Merge fields are simplest, Flow adds logic and queries without code, and Apex covers what Flow cannot do cleanly.
What each resource does well
| Resource | Best for | Main cost |
|---|---|---|
| Merge fields | Fields on the input record and its parents, such as `{!$Input:Case.Account.Name}` | Cannot filter, sort, or combine data |
| Flow | Child records, filtered lists, branching, or data from several objects | Needs a Flow that returns text, and large Flows are harder to maintain |
| Apex | Complex queries, loops, string formatting, or logic that is hard to express in Flow | Needs a developer, tests, and deployment |
Merge fields
Choose merge fields when the value lives on the input record or on a record reachable by a relationship field. Examples are the case subject, the account industry, and the contact name. They need no extra component, and the preview shows exactly what was inserted.
Merge fields stop working when you need several child records, such as the last five orders. They also cannot apply a filter such as "open tasks only".
Flow
Choose Flow when the data needs a query or a decision, but you can build it with Flow elements. A Flow can run Get Records, filter and sort the results, loop through them, and build a text variable. The template inserts that text with a line such as {!$Flow:Get_Recent_Orders.Prompt}.
Flow suits admin-owned logic. An admin can change the filter without a code deployment.
Apex
Choose Apex when Flow becomes awkward. Typical signs are nested loops, heavy string formatting, aggregate queries, or logic that you want to unit test. The invocable method in the earlier example returns a Prompt string, so the template treats it like any other resource.
A selection path
Work through these questions in order and stop at the first "yes".
- Is the value a field on the input record or a parent record? Use a merge field.
- Can an admin build the lookup, filter, and text formatting with Flow elements? Use a Flow.
- Does the logic need code, or do you need unit tests and tight control of the query? Use Apex.
Worked example
A support team wants a case summary prompt. They list their data needs and choose a resource for each.
| Data needed | Where it lives | Resource |
|---|---|---|
| Case subject, status, description | Input Case record | Merge field |
| Account name and industry | Parent Account | Merge field with relationship |
| Last three orders for the account | Child records, sorted | Flow |
| Open tasks for the case, formatted as a list | Child records, filtered, formatted | Flow, or Apex if the formatting grows complex |
The final template mixes all of these. One template can use merge fields, a Flow resource, and an Apex resource together.
Customer account: {!$Input:Case.Account.Name}
Recent order history:
{!$Flow:Get_Recent_Orders.Prompt}Common errors in choosing
- Reaching for Apex first. Code adds deployment and test work. Start with merge fields and move up only when you must.
- Building a Flow for a parent field. A relationship merge field gives the same value with no Flow to maintain.
- Returning too much text. Flow and Apex can return large blocks. Limit the rows, for example with
LIMIT 10, because long grounding text uses prompt space and can bury the instruction. - Ignoring security. Apex declared
with sharingand queries usingWITH USER_MODErespect the running user's access. A Flow's access depends on how it runs, so check that too. - Returning an empty string. If the lookup finds nothing, return a clear line such as "No open tasks." so the model does not guess.
Quick reference
- Fewer moving parts means fewer failures. Prefer merge fields.
- Flow keeps logic in the admin's hands.
- Apex gives full control and testability at the cost of code ownership.
Exercise
A template needs the account's industry, the three most recent closed cases for that account (newest first), and a one-line summary of each. Which resource would you pick for each part, and why?
Show answer
The account industry is a parent field, so use a merge field such as {!$Input:Case.Account.Industry}. The three most recent closed cases need a query with a filter, a sort, and a limit, so use a Flow that runs Get Records and builds a text variable. The one-line summaries can be built in the same Flow. Move to Apex only if the formatting or query logic becomes too complex for Flow to express cleanly.
Exercise
Your Apex grounding method returns an empty string when a case has no open tasks. What problem can this cause, and what is the fix?
Show answer
An empty value leaves a blank spot in the prompt, and the model may guess or invent task details. Return an explicit line such as "No open tasks." and keep an instruction in the template that says to state when a value is not provided.