Sub topic 6
Pulling in related records
A prompt that only knows the fields of one record often lacks context. A case summary improves when the model also sees the customer's account and the contact who raised the case. This chapter shows how to reach those related records from a template with relationship fields.
This chapter assumes you know the Salesforce data model (objects, lookup and master-detail relationships), have written a prompt template with record merge fields, and know which record anchors a template.
How relationship fields work
A template has an input record, such as a Case. Every merge field starts from that record. A relationship field follows a lookup from the input record to a parent record and then reads a field on the parent.
The syntax is the merge field you already know, with the relationship name added to the path:
Customer account: {!$Input:Case.Account.Name}
Account industry: {!$Input:Case.Account.Industry}
Contact: {!$Input:Case.Contact.Name}Each line reads as follows:
$Input:Caseis the input record.AccountorContactis the relationship name of the lookup field.NameorIndustryis the field on the parent record.
The result is the same as if you had pasted the parent's values into the template by hand, but the platform resolves them for each record at run time.
Build the template
Combine the case's own fields with the related values. Tell the model what to do when a value is missing, because a lookup can be empty.
You are a support assistant. Write a two-sentence briefing for an agent taking over this case.
Case subject: {!$Input:Case.Subject}
Priority: {!$Input:Case.Priority}
Customer account: {!$Input:Case.Account.Name}
Account industry: {!$Input:Case.Account.Industry}
Contact: {!$Input:Case.Contact.Name}
If a value above is empty, say it is not provided. Do not guess.The last line matters more here than in a template with only record fields. Cases created from email or web forms often have no Account or Contact. In that case the merge field resolves to an empty value, and without the instruction the model may invent a customer.
Use the field picker
In Prompt Builder, you do not need to type the paths by hand. Insert a resource from the editor, choose the input object, and then expand a lookup field to see the fields on the parent. This avoids typos in relationship names.
Relationship names are not always the same as the field label. A custom lookup field named Primary_Vendor__c has the relationship name Primary_Vendor__r. Use the picker or check the object in Object Manager when a path fails to resolve.
What relationship fields can and cannot do
Relationship fields follow a lookup from the child record up to one parent. They suit data that has a single value per record.
| Need | Relationship field works? |
|---|---|
| The account name for a case | Yes, a parent with one value |
| The account owner's name, two lookups up | Usually, if the path is supported for that object; check in the picker |
| The last five orders for the account | No, this is a list of child records |
| Open tasks for the case | No, this is a list of child records |
| A count or sum across children | No, this needs a query or calculation |
The second row is the common surprise. Paths can chain across more than one lookup, but support depends on the object and the picker. Treat any path the picker does not offer as unsupported, and do not assume it will work.
The last three rows describe data that sits below the record, as a list or an aggregate. Merge fields cannot query those. For them you need a Flow or Apex resource, which the next chapters cover.
Common errors
- The value appears blank in the preview. The lookup on the input record is empty, or the running user cannot see the parent record or field. Open the record and check both.
- The raw merge field appears in the output. The path is wrong, often because of a misspelled relationship name or a custom
__rname. Re-insert the field with the picker. - The model states the wrong customer. The template lacks an instruction for empty values, so the model filled the gap. Add the "do not guess" line.
- Sensitive parent data appears. A relationship field gives the model whatever the field holds. Include only the parent fields the task needs.
Keep it small
Each related field adds text to the prompt. Add the fields the model needs for the task, such as the account name and industry for a briefing, and leave out the rest. A shorter prompt costs less, fits within size limits, and gives the model less to get wrong.
Exercise
A template uses {!$Input:Case.Account.Name} and the preview shows an empty value for one case, while other cases show a name. Give two possible causes and one change to the template that makes the output safer.
Show answer
Either the Account lookup on that case is empty, or the user running the preview lacks access to that Account or its Name field. To make the output safer, add an instruction such as "If a value above is empty, say it is not provided. Do not guess," so the model does not invent a customer name.
Exercise
You want a case briefing to include the customer's five most recent orders. Can you do this with relationship fields alone? Explain.
Show answer
No. Relationship fields read single values from a parent record. Recent orders are a list of child records that need a query, so you use a Flow or Apex resource to return them as text.