Sub topic 8
Flex templates and flexible inputs
Flex templates are the prompt template type that is not tied to one object or one place in the user interface. You define the inputs yourself and decide where the output goes. This chapter covers how Flex differs from the object-bound types, how inputs and merge fields work, and how a Flex template is invoked.
This chapter assumes you know the following:
- Salesforce objects, records, fields and relationships.
- How to navigate Setup and Prompt Builder.
- The anatomy of a prompt template (name, type, prompt text, model) and basic merge field grounding, both covered in earlier chapters.
What makes Flex different
Sales Email, Field Generation and Record Summary each fix two things:
- The input. Each is built on one object. A Sales Email template takes a Contact or Lead and related records. A Field Generation template takes the record that holds the target field. A Record Summary template takes the record whose page it will appear on.
- The output location. The email composer, the field, and the record page are each built in. Salesforce knows where to show the result.
A Flex template fixes neither. You choose the inputs, and the template does not decide where its output appears. That makes Flex the general-purpose type, and it is why the spine's Flex example, a Case summary, can also be written as a Record Summary.
| Aspect | Object-bound types | Flex |
|---|---|---|
| Number and kind of inputs | One primary object, set by the type | Defined by you, and can include more than one record |
| Merge field source | The base object | The names you give your inputs |
| Output location | Built in (email composer, field, record page) | Whatever calls the template |
| Typical invoker | A Salesforce UI feature | Flow, Apex, or the REST API |
Inputs and the merge field syntax
When you create a Flex template, you add one or more inputs. Each input has:
- A name. You choose it, and it appears in every merge field that uses the input.
- A type. In the common case this is a record of a specific object, such as Case, Account or a custom object. Depending on your release, other input types, such as free text, may also be offered.
The name you give an input becomes part of the merge field. The pattern is {!$Input:InputName.FieldName}. For an input named Case, the Subject field is {!$Input:Case.Subject}. If you name the input SupportCase, the same field becomes {!$Input:SupportCase.Subject}.
This is the main practical difference from the object-bound types. In those types the input name is predetermined, so you pick fields from a list. In Flex you control the names, so they should be short, readable and consistent across your templates.
Here is the case summary prompt from the spine. It works as a Flex template with one input named Case:
Summarize this case for a support manager in three bullet points: the problem, what has been tried, and the next step.
Subject: {!$Input:Case.Subject}
Description: {!$Input:Case.Description}When the template runs, Salesforce replaces each merge field with the value from the record that was passed in. The model receives real text, not placeholders.
Using more than one input
A single Flex template can take several inputs, so one prompt can ground itself in several unrelated records. A renewal brief might take an Account and an Opportunity, for example:
You are helping a renewals manager prepare for a call.
Write a five-sentence briefing.
Account: {!$Input:Account.Name}
Industry: {!$Input:Account.Industry}
Opportunity: {!$Input:Opportunity.Name}
Stage: {!$Input:Opportunity.StageName}
Close date: {!$Input:Opportunity.CloseDate}
If a value is empty, leave it out instead of guessing.Here the Account and Opportunity are two separate inputs. Nothing forces the Opportunity to be a child of the Account. The caller passes in whichever two records it wants, and your prompt, not the platform, defines the relationship between them. The upside is flexibility. The downside is that a caller can pass a mismatched pair, and the model will happily write about it.
Related data and other resources
Merge fields are not the only way to ground a Flex prompt. As with the other types, you can add resources, such as related lists reached through the input record, or the output of a Flow or an Apex class. Resources are useful when the data you need is not a simple field on the input, such as the last ten activities or a calculated total. Use the resource picker in the prompt editor to insert them, so the syntax is generated for you.
Where Flex output goes
A Flex template does not write itself into a field or onto a page. It returns generated text to whoever invoked it, and that caller decides what to do next. The usual callers are:
- Flow. A Flow can call the prompt template through an action, then save the text to a field, create a Task or Chatter post, or send an email. This is the lowest-code route.
- Apex. Code can call the Connect API to generate a response and then process it.
- REST API. An external system can request a generation and receive the text.
Activation and invocation are covered in a later chapter. For now, remember that a Flex template without a caller does nothing in production, and that the template is only one piece of the solution.
Invoking a Flex template from Apex
The following Apex invokes a Flex template with one Case input. Replace Case_Summary with the API name of your own template, and pass a real Case Id. The key Input:Case must match the input name you defined in the template.
public with sharing class CaseSummaryService {
public static String summarize(Id caseId, String templateApiName) {
Map<String, ConnectApi.WrappedValue> inputParams =
new Map<String, ConnectApi.WrappedValue>();
ConnectApi.WrappedValue caseInput = new ConnectApi.WrappedValue();
caseInput.value = new Map<String, String>{ 'id' => caseId };
inputParams.put('Input:Case', caseInput);
ConnectApi.EinsteinPromptTemplateGenerationsInput request =
new ConnectApi.EinsteinPromptTemplateGenerationsInput();
request.additionalConfig = new ConnectApi.EinsteinLlmAdditionalConfigInput();
request.additionalConfig.applicationName = 'PromptBuilderPreview';
request.isPreview = false;
request.inputParams = inputParams;
ConnectApi.EinsteinPromptTemplateGenerationsRepresentation result =
ConnectApi.EinsteinLLM.generateMessagesForPromptTemplate(
templateApiName, request);
return result.generations[0].text;
}
}You would call it as CaseSummaryService.summarize(someCaseId, 'Case_Summary');. The method returns plain text. Your code is responsible for storing it, for example by updating a custom Long Text field.
Design decisions specific to Flex
Because Flex gives you freedom, a few choices matter more than they do with the other types.
Name inputs for the reader
Use Case, Account and Opportunity rather than Input1 and Input2. The merge fields appear in the prompt text, and a person reviewing the template should see at once what each one refers to.
Keep the prompt independent of the output location
The prompt should describe the content, not the place it ends up. If a Flow stores the text in a field with a 255-character limit, put that limit in the prompt ("Write at most 200 characters") instead of assuming the platform will trim it. Flex gives no built-in truncation or formatting for a destination.
Decide what happens with missing values
An empty merge field typically renders as an empty string. The prompt should say what to do, as in the examples above ("If a value is empty, leave it out instead of guessing"). Otherwise the model may fill the gap with invented content.
Check access with the running user
Merge fields resolve with the permissions of the context that runs the generation. A caller who cannot see a field may get an empty value. When a Flex template works in a test run but returns thin results from a Flow, compare the running user's field-level security and record access with the user who tested it.
Common errors
- Merge field renders as literal text. The input name in the merge field does not match the input name defined on the template. Check spelling and capitalization.
- Blank values in the output. The record Id passed in was wrong, the record lacked the field value, or the running user cannot see the field.
- Invocation fails with an input error. The caller supplied the wrong number of inputs or the wrong key. Every input defined on the template must be supplied.
- Output is too long or badly formatted for its destination. The prompt does not state the length or format. Add it explicitly.
- Template works in Preview but not in production. Preview lets you choose the record yourself. The live caller may pass something different, such as a record of another type. Test the real caller, not only the preview.
When Flex is the right choice
Choose Flex when at least one of these holds:
- The prompt needs records from more than one object.
- The result must go to a place with no built-in template type, such as a Chatter post, a Task, an external system, or a custom object field.
- Automation, not a person clicking in a page, triggers the generation.
Choose an object-bound type when the scenario is a plain email draft, a single field, or a summary on a record page, because those types provide the entry point for you. The next chapter compares all four types side by side.
Exercise
A support team wants a prompt that takes a Case and its related Account and writes a two-sentence handoff note. A Flow will then save the note to a custom field on the Case. (a) Which template type fits, and why? (b) If you name the Case input Case, what merge field refers to the Case subject? (c) Name one thing the prompt text should say about missing data.
Show answer
(a) Flex, because the prompt needs two record inputs and the output goes to a custom field through a Flow, a destination that no object-bound type provides. (b) {!$Input:Case.Subject}. (c) It should tell the model to leave out any empty value rather than guess, for example "If a value is empty, leave it out instead of guessing."
Exercise
You rename the input Case to SupportCase in a new version of a Flex template that an Apex class already calls. What breaks, and how do you fix it?
Show answer
The Apex class passes the input under the key Input:Case, which no longer matches the template's input name, so the call fails or the merge fields come back empty. Fix it by changing the key in the Apex code to Input:SupportCase, or by keeping the original input name in the new version. Test the real caller before activating the new version.