Sub topic 5
Grounding with related lists, Flow and Apex
This chapter assumes you can already build a basic Flex prompt template with merge fields for a single record, as covered in the previous chapter. It also assumes you know how to read a Flow and an Apex class.
Why a single record is not enough
Merge fields such as {!$Input:Case.Subject} only reach fields on the record you pass in. A good case summary also needs the comments, emails, or order history that hang off that case. Without them, the model fills the gaps with guesses.
Grounding means giving the model the facts it needs inside the prompt. Prompt Builder offers three ways to add data beyond the input record:
| Resource | Use it when | Who shapes the data |
|---|---|---|
| Related list | The data is a child list of the input record | Prompt Builder, with a field selection |
| Flow | You need queries, filters, or logic across objects, built declaratively | A flow author |
| Apex | You need logic or formatting that Flow cannot express | A developer |
Related lists
A related list resource returns the child records of the input record, for example the comments on a case. In the template editor, you insert the resource from the Insert Resource menu and choose the related list and the fields to include.
Prompt Builder inserts a merge field for you. Do not type it from memory, because the exact name depends on the relationship. The field you pick controls how much text reaches the model, so choose only fields the prompt needs.
Case comments:
{!$RelatedList:Case.CaseComments.Records}
Rules:
- Use only the comments above.
- If there are no comments, say that no comments exist.Keep these limits in mind:
- Each related list adds to the prompt length, and the model has a size limit.
- The list respects the running user's access to the child object and its fields.
- An empty list produces an empty section, so say in the rules what the model should do then.
Flow resources
Use a Flow when the data needs a query that a plain related list cannot express. An example is "open cases for the same account in the last 30 days". The flow must be autolaunched. Its input variables receive values from the template, and it returns text that the template inserts.
Choose the flow from the Insert Resource menu so that Prompt Builder shows only flows that qualify. Those flows follow the output convention Prompt Builder expects. Test the flow on its own first, because a flow fault during generation surfaces as a failed or empty prompt section.
Apex resources
Use Apex when you must combine data from several sources, apply formatting, or call code you already own. The class exposes an invocable method, and the template calls it as a resource.
public with sharing class CaseHistoryPrompt {
public class Request {
@InvocableVariable(required=true)
public Case RelatedEntity;
}
public class Response {
@InvocableVariable
public String Prompt;
}
@InvocableMethod(
label='Recent Cases for Account'
CapabilityType='FlexTemplate://Summarize_Case')
public static List<Response> run(List<Request> requests) {
List<Response> out = new List<Response>();
for (Request r : requests) {
List<Case> recent = [
SELECT CaseNumber, Subject, Status
FROM Case
WHERE AccountId = :r.RelatedEntity.AccountId
AND Id != :r.RelatedEntity.Id
ORDER BY CreatedDate DESC
LIMIT 5
];
List<String> lines = new List<String>();
for (Case c : recent) {
lines.add(c.CaseNumber + ': ' + c.Subject + ' (' + c.Status + ')');
}
Response res = new Response();
res.Prompt = lines.isEmpty()
? 'No other recent cases.'
: String.join(lines, '\n');
out.add(res);
}
return out;
}
}The class uses with sharing so the query respects the user's record access. The LIMIT keeps the prompt small. The method is bulk-safe because it loops over the whole request list.
The CapabilityType value names the template this method serves. Confirm the exact attribute and variable names against the Prompt Builder documentation for your release, since they have changed between releases.
Common errors
- A related list is added, but the output ignores it. The prompt text never tells the model to use it. Add a rule such as "Use the comments above".
- The Flow resource does not appear in the menu. The flow is not autolaunched or lacks the expected output.
- The Apex output is empty for some users. Sharing or field access hides the records from them.
- The prompt is too long. Reduce the related-list fields or the
LIMIT.
Exercise
A case summary template shows only the case subject and status. You want it to mention the five most recent cases from the same account, excluding the current one. Which resource do you choose, and why not a related list?
Show answer
Use a Flow or Apex resource. A related list returns only the child records of the input record, so it cannot return other cases from the same account while excluding the current case. A flow or an Apex query can filter by account and limit the count to five.