Sub topic 4
Inputs, resources and merge fields
This chapter assumes you know Salesforce objects, fields and records, and you have seen a prompt template in Prompt Builder. It covers how a template receives data, how that data gets into the prompt text, and what goes wrong when it doesn't.
Why this matters for an agent action
A prompt template is only as good as the data in it. The model has no access to your org. It sees the text that Prompt Builder assembles at run time. If a field is not placed in that text, the model does not know it. When the model does not know a fact, it may invent one.
Three terms describe how data reaches the prompt:
- An input is a value the caller supplies when the template runs. For example, a Case record.
- A resource is a source of data that the template can read from. Inputs are one kind. Related lists, Flow outputs and Apex outputs are others.
- A merge field is a placeholder in the prompt text, written as
{!...}. At run time, Prompt Builder replaces it with the real value. This process is also called resolving the merge field.
When the template is an agent action, the caller is the agent. The agent decides what to pass in, and the template turns that into a fully written prompt.
How a template is resolved
Prompt Builder resolves a template in a fixed order. It first receives the inputs. It then evaluates every merge field in the template body, including any Flow or Apex resources. Next, it sends the finished text through the Einstein Trust Layer, which later chapters cover. Last, it sends the text to the model.
This order explains most debugging. If the output is wrong, the first question is whether the resolved prompt contained the right data. The model is only the second suspect.
Defining inputs
In a Flex template, you declare inputs in the template's configuration. Each input has a name, a type, and a flag for whether it is required. The type is usually one of two kinds:
- A record input is a Salesforce object such as Case, Account or Contact. The caller passes a record, and the template can read the fields of that record.
- A text or other value input is a plain value, such as a string. Use it for things that are not stored on a record, like a tone requested by the user.
Other template types use fixed inputs. A record-triggered type, for example, is tied to one object, and the object is chosen when you create the template. Chapter 3 covered how types differ. The point here is that Flex gives you the freedom to define inputs yourself, so it asks you to be explicit.
A good rule is to keep inputs few and meaningful. A template with one record input, such as Case, can pull in everything else through that record. A template with ten text inputs forces the agent to find and pass ten values correctly, and each one is a place for the agent to make a mistake.
Merge field syntax
A merge field has a prefix that names the kind of resource, then a path to the value. The examples below show the common forms.
| Merge field | Reads from |
|---|---|
| `{!$Input:Case.Subject}` | A field on the Case input record |
| `{!$Input:Case.Account.Name}` | A field on a parent record reached through a lookup |
| `{!$RelatedList:Case.CaseComments.Records}` | The records in a related list of the input |
| `{!$Flow:Get_Case_History.Prompt}` | The text output of an autolaunched Flow |
| `{!$Apex:CaseContextBuilder.Prompt}` | The text output of an invocable Apex method |
The names inside the braces are API names, not labels. Use the resource picker in the template editor to insert them instead of typing them. The picker lists only what is valid for the template, so it avoids typos. A misspelled merge field is a common cause of a prompt that silently contains nothing where the data should be.
Here is a template that uses inputs and rules together:
You are a service assistant. Write a short, factual summary of the case below for a customer call hand-off.
Case number: {!$Input:Case.CaseNumber}
Subject: {!$Input:Case.Subject}
Status: {!$Input:Case.Status}
Description: {!$Input:Case.Description}
Rules:
- Use at most five sentences.
- Use only the information above. If something is missing, say it is unknown.
- Do not promise refunds or dates.Each line with a merge field has the same structure: a plain label, then a placeholder. The label is for the model's benefit. After resolution, the model sees something like Status: Escalated, and the label tells it what that value means. If you wrote only {!$Input:Case.Status} with no label, the model would see a bare word.
What a resolved prompt looks like
Suppose the input is a Case with number 00001042, the subject "Invoice shows wrong tax", status "Escalated", and an empty description. The model would receive this:
You are a service assistant. Write a short, factual summary of the case below for a customer call hand-off.
Case number: 00001042
Subject: Invoice shows wrong tax
Status: Escalated
Description:
Rules:
- Use at most five sentences.
- Use only the information above. If something is missing, say it is unknown.
- Do not promise refunds or dates.The Description line is empty. The template does not warn you about this. The rule "If something is missing, say it is unknown" handles it, and that rule is the reason it exists. Without it, a model might fill the gap with a plausible guess. When you write a template, assume some merge fields will be empty and say what should happen.
Related lists, Flow and Apex resources
The record input gives you the fields of one record. Often you also need data from other records. The three sources differ in effort and control.
Related lists are the simplest. You add a related list of the input object, such as case comments or contacts, and Prompt Builder inserts the records. You choose which fields to include and, in the editor, how many records. Use this when the data is a direct child of the input and you want most of it.
Flow resources let you shape the data first. An autolaunched Flow can query several objects, filter or sort them, and return one text value. Use a Flow when the logic is a filter you can express without code, such as "the last three closed cases for this account".
Apex resources give full control. An invocable Apex method can run SOQL, combine results and format the output. Use Apex when the logic is complex, or when you must apply rules a Flow cannot express easily.
Whichever you choose, remember that the output is inserted into the prompt as text. A Flow or an Apex method that returns a very large result makes the prompt large. This costs tokens and can push important instructions out of the model's useful range. Return only what the task needs.
Access and data limits
Merge fields read data on behalf of the running user. A user who cannot see a field, or a record, should not see it in the generated text. When a template works for you in preview but returns blanks for a colleague, check field-level security and record sharing for that colleague before you change the template. For agents, check which user the agent runs as, and what that user can access. Chapter 11 covers testing, but this check is worth remembering now.
Long text fields are another limit. A Description of several thousand words becomes thousands of tokens of prompt. Models also have a context window, which is a maximum amount of text they accept. If a field can be very long, consider using a Flow or an Apex resource to trim it.
Common errors
These are the problems you will meet most often, with the usual cause:
- The output says information is missing, but the record has it. The merge field is wrong, points to a different field, or the user cannot read the field. Look at the resolved prompt in preview.
- The output contains a made-up fact. A field was empty and the template had no rule for that case. Add an explicit instruction for missing data.
- The template works in preview but not as an agent action. The agent did not pass the input, or passed the wrong record. Chapter 9 covers input mapping.
- A related list returns too much. Limit the fields and the record count, or use a Flow to filter.
- The label and value are mismatched. You edited a merge field but left an old label next to it. Read the resolved prompt top to bottom.
A short working checklist
- Decide the single record the task is about, and make it the input.
- List the facts the model needs. Mark which are on the record, on related records, or need logic.
- Use fields first, a related list second, a Flow third, and Apex last. Each step adds complexity.
- Put a label in front of every merge field.
- Write a rule for what the model does when a value is missing.
- Preview with a record that has the data and one that does not.
Exercise
A Flex template has the line Priority: {!$Input:Case.Priority}. In preview, the model's summary says the priority is unknown, but the Case record shows High. Name two checks you would make before editing the prompt wording.
Show answer
First, open the resolved prompt and see whether the Priority line contains a value. If it is blank, the merge field or the input is the problem, not the model. Second, check whether the user running the preview has field-level security access to Priority on Case. Also confirm the API name in the merge field matches the field. Only if the resolved prompt contains "High" should you change the wording or rules.
Exercise
You need a template to include the last three closed cases for the case's account, newest first. Which resource type would you choose, and why not a plain related list?
Show answer
Use a Flow resource, or Apex if the logic outgrows Flow. A related list of the input object cannot express "closed cases of the parent account, newest three". A Flow can query the account's cases, filter by status, sort by close date and limit the result, then return it as text for the prompt.