Sub topic 6
Pulling in related records
This chapter covers how to bring data from records connected to your anchor record into a prompt template. It assumes you can write record merge fields like {!$Input:Case.Subject} and know how lookup and master-detail relationships work in Salesforce. It also assumes you know what a parent and a child record are. The previous chapter showed how the input record becomes the base of a template. This chapter extends that record outward.
Why the anchor record is rarely enough
A Case record holds a subject, a status and a description. A useful summary usually needs more than that. An agent taking over a case wants to know which account it belongs to, what industry that account is in, and who the contact is. Those facts live on other records.
Salesforce stores them on related objects, and a template can reach them through relationship fields. You don't have to write code or build a flow for these fields. You use the same merge-field syntax you already know and add the relationship to the path.
The two directions of a relationship
Relationships point in two directions, and the direction decides how you ground the prompt.
- A parent is the record on the "one" side of a lookup or master-detail relationship. A Case has one Account and one Contact. From the Case, each of these is a single record, so a field path works.
- A child is a record on the "many" side. An Account has many Cases, Opportunities and Contacts. From the Account, these are a list, and a single field path cannot name a list.
This difference drives the rest of the chapter. Parent data fits merge fields. Child data needs something that handles lists.
Following a relationship from the input record
The input record is the record you pass to the template. For a Case template it is $Input:Case. To read a parent, you add the relationship name to the path, then the field name.
Customer account: {!$Input:Case.Account.Name}
Account industry: {!$Input:Case.Account.Industry}
Contact: {!$Input:Case.Contact.Name}Each piece of the path has a job:
$Input:Casenames the input record.Accountis the relationship name. For a standard lookup it is the lookup field's name without the trailingId.AccountIdbecomesAccount.NameorIndustryis the field on the parent record.
For a custom lookup field, the relationship name ends in __r. A lookup field called Primary_Vendor__c is followed through Primary_Vendor__r. You normally don't type these by hand. The resource picker in Prompt Builder lists the parent fields and inserts the correct path for you. Use the picker for custom relationships, because a typo in the path will not give you the field you meant.
A complete template using parent data
This template combines fields from the Case with fields from two parents. It also tells the model what to do when a parent is missing.
You are a support assistant. Write a three-sentence handover note for the agent taking over this case.
Case subject: {!$Input:Case.Subject}
Status: {!$Input:Case.Status}
Priority: {!$Input:Case.Priority}
Customer account: {!$Input:Case.Account.Name}
Account industry: {!$Input:Case.Account.Industry}
Contact: {!$Input:Case.Contact.Name}
Mention the account and industry in the first sentence.
If the account or contact is empty, say it is not provided. Do not guess.The last line matters. A Case does not have to have an Account or a Contact. Cases created from email or web forms often have neither at first.
What happens when a parent is empty
When the lookup is empty, the merge field has no record to read from. The field resolves to an empty value, not an error. The prompt that reaches the model then looks like this:
Customer account:
Account industry:
Contact: The model sees labels with nothing after them. Without an instruction, it may invent a plausible account name, or it may write around the gap in a way that sounds confident. This is the reason for the "say it is not provided" line in the template. The template cannot change the data, so the wording of the instruction has to cover the case where the data is missing.
You can see this behavior directly in preview. Pick a Case with no Account and read the resolved prompt. The chapter on previewing covers that workflow in full.
Choosing which parent fields to include
Each field you add costs space in the prompt and adds something the model can attend to. A parent record can have dozens of fields. Include only those that change the answer.
| Field | Include? | Reason |
|---|---|---|
| `Account.Name` | Yes | Identifies the customer in the output |
| `Account.Industry` | Often | Shapes tone and examples |
| `Account.Description` | Rarely | Long free text that can drown out the case |
| `Account.BillingStreet` | No | Not relevant to a summary, and adds personal data |
| `Contact.Name` | Yes | Lets the note address the right person |
| `Contact.Email` | Only if needed | Adds personal data without helping a summary |
A good rule is to ask, for each field, what the model would write differently without it. If the answer is "nothing", leave it out. Personal data you don't need is also a risk. If the output only has to mention the contact by name, don't send the contact's email address and phone number.
Child records need a different resource
A merge field path ends at one field on one record. You cannot write a path that means "all Opportunities for this Account", because the result is a list of unknown length.
You have three practical options for child records:
- A related list resource. For some record-based template types, the resource picker offers related lists, such as the Contacts or Opportunities of an Account. Check whether your template type lists them. If the picker shows one, use it, because it needs no extra build work.
- A Flow resource. The flow queries the child records, filters and sorts them, and returns a text block. The template inserts it with a line such as
{!$Flow:Get_Recent_Orders.Prompt}. This is the subject of the next chapter. - An Apex resource. An invocable Apex method does the same job in code when the logic is too involved for a flow.
Pick the simplest option that gives you the filtering you need. Related lists give you what the picker offers and little control. A flow gives you filtering, sorting and a row limit without code.
Why child lists need a limit
An Account with 4,000 Cases would produce a prompt far larger than the model's input allows, or one that is mostly noise. Any approach for child data should limit the rows and choose which ones. Typical choices are the most recent ten, only open records, or only records above a value threshold. A flow or Apex method applies these with a query filter, a sort order and a row limit. The Apex example in the spine does this with LIMIT 10 and IsClosed = false.
Gotchas with related data
Related data adds several failure modes that field-only templates don't have.
- Field-level and record-level access. The user running the template may not have access to a parent record or one of its fields. The value can then come back empty even though the data exists. The prompt looks the same as a missing parent, so the model cannot tell the two apart.
- Stale or sparse parents. An Account with no Industry value gives you an empty field. Many accounts in a typical org have gaps like this. The template should read well when any single value is blank.
- Long text on parents. Free-text fields such as descriptions can be several thousand characters. They count against the prompt's size limit and can bury the lines you care about.
- Lookups that point to different objects. Some fields, such as
WhoIdandWhatIdon Task, can point to more than one object type. A path that works for one type does not work for the other. Check the picker for what is offered for the field. - Deep chains. A path such as Case to Contact to Account to Owner is possible in some cases, but each step adds a way to get an empty result. Prefer a short path. If you need a distant record, a flow can fetch it explicitly.
Write instructions that name the related data
Once parent data is in the prompt, tell the model how to use it. A template that lists fields but never refers to them often yields output that ignores them.
Weak:
Summarize the case.
Account: {!$Input:Case.Account.Name}
Industry: {!$Input:Case.Account.Industry}Better:
Summarize the case in two sentences. Name the account in the first sentence and refer to its industry when it explains the urgency. Use only the facts below.
Account: {!$Input:Case.Account.Name}
Industry: {!$Input:Case.Account.Industry}The second version tells the model where each value belongs and limits it to the supplied facts. Both points reduce guessing.
A checklist before you move on
- Identify which related records the output depends on.
- Mark each as a parent (single record) or a child (list).
- Use relationship merge fields for parents, and the picker for custom relationships.
- Use a related list, a flow or Apex for children, with a filter and a row limit.
- Add an instruction for each value that might be empty.
- Preview with a record that has the parents and one that doesn't.
Exercise
A Case template uses {!$Input:Case.Account.Industry}. For one Case, the resolved prompt shows "Account industry: " with nothing after it. Name two different causes, and say what you would change in the template so the model does not invent an industry.
Show answer
The first cause is that the Case has no Account, or the Account's Industry field is blank. The second is that the running user lacks access to the Account or to the Industry field, so the value resolves to empty. In the template, add an instruction such as "If a value is empty, say it is not provided. Do not guess." If the output must tell the two causes apart, move the lookup into a flow or Apex method that returns explicit text for each case.
Exercise
You want a prompt for an Account to include its ten most recent open Opportunities. Why can't you do this with a merge field path, and which resource would you try first?
Show answer
Opportunities are child records of the Account, so they form a list. A merge field path ends at one field on one record and cannot express a filtered, sorted list with a row limit. Try a related list resource first if the template type offers one and its filtering is enough. Otherwise build a Flow that queries open Opportunities sorted by date with a limit of ten and returns a text block, then insert it with a {!$Flow:...} line.