Sub topic 12
Activating Templates and Invoking Them from Salesforce
This chapter covers what happens after you finish writing and testing a template: you activate a version, and then something in Salesforce calls it. It assumes you know the four template types (Sales Email, Field Generation, Record Summary, Flex), merge fields, and how previewing works. It also assumes basic familiarity with Flow Builder and Apex.
Why activation is a separate step
A template is only a definition until a version of it is active. Each template has one or more versions, and each version is either Draft or Active. Drafts can be edited and previewed in Prompt Builder. They cannot be called from anywhere else.
Three rules cover most of the behavior:
- Only one version of a template is active at a time.
- An active version is read-only. To change it, you create a new version from it, edit the copy, test it, and then activate the copy.
- Activating a new version makes the previous active version inactive. Every caller that refers to the template by name gets the new version immediately.
The third rule matters. Callers reference the template, not a specific version, so activation works like a deployment. A bad version that you activate goes live for every user and flow that calls the template.
What an active template can do
Activation makes the template available. What happens next depends on the type. The type decides where Salesforce can surface the template, and the table shows the usual places.
| Template type | Typical place it runs | Who triggers it |
|---|---|---|
| Sales Email | Email composer on a record page | A user, who reviews the draft before sending |
| Field Generation | A field on a Lightning record page | A user, who reviews the text before saving |
| Record Summary | A record page or an agent action | A user or an agent |
| Flex | Flow, Apex, REST API, agent actions | Automation or code |
Exact surfaces change between releases, so confirm them in the current Salesforce release notes. The pattern stays the same. The three object-bound types are built for an interactive place in the UI. Flex is built for callers that supply their own inputs.
Invoking from the user interface
Interactive invocation needs configuration outside Prompt Builder. Activating a Field Generation template does not by itself put anything on a page.
For a Field Generation template, you edit the record page in Lightning App Builder and place the target field using Dynamic Forms. You then associate the active template with that field. When a user opens the record, they can generate a value for the field, read it, and save or discard it. The model never writes to the record on its own.
For a Sales Email template, the template appears as an option in the email composer for records of its object. The user generates a draft, edits it, and sends it. Because a human reviews the text, a Sales Email template is a good starting point for a first rollout.
Whatever the type, the user running the template needs permission. Building templates needs the Prompt Template Manager permission set. Running them needs the Prompt Template User permission set. The user also needs read access to every record and field the template merges in. A common problem is a template that previews well for the admin and returns blanks for a sales user. The usual cause is missing field access, not a defect in the prompt.
Invoking from Flow
Flow is the main no-code route for automation. In Flow Builder, add an Action element and look for the prompt template actions. The action lets you pick an active template and supply its inputs.
The steps are:
- Choose the active template by name.
- Map each template input. For an object-bound template, this is usually a record variable or a record ID. For a Flex template, you map every input the template defines.
- Store the action's output in a variable. The output is text, typically named for the prompt response.
- Use that variable in a later element. You might update a record, create a task, post to Chatter, or send an email.
A flow can run unattended, so no one reviews the text before it is used. Design for that. Write the result to a field meant for AI output, or create a draft or task, instead of overwriting existing data. Also use a Decision element to skip the call when the inputs are empty. Sending a prompt with no data wastes a call and often produces vague text.
Flex templates fit Flow best because the flow can pass any input, including values it computed earlier. A Flex template named Case_Triage with a Case input could run in a record-triggered flow when a case is created, then store a three-bullet summary on a custom field.
Invoking from Apex
Apex gives you the same call with more control over inputs and error handling. The Connect API exposes ConnectApi.EinsteinLlm.generateMessagesForPromptTemplate. You pass the template's API name and an input object that carries the input values.
public with sharing class AccountSummaryService {
public static String summarize(Id accountId) {
ConnectApi.EinsteinPromptTemplateGenerationsInput input =
new ConnectApi.EinsteinPromptTemplateGenerationsInput();
input.additionalConfig = new ConnectApi.EinsteinLlmAdditionalConfigInput();
input.additionalConfig.applicationName = 'PromptBuilderPreview';
input.isPreview = false;
Map<String, String> recordRef = new Map<String, String>();
recordRef.put('id', accountId);
ConnectApi.WrappedValue wrapped = new ConnectApi.WrappedValue();
wrapped.value = recordRef;
Map<String, ConnectApi.WrappedValue> params =
new Map<String, ConnectApi.WrappedValue>();
params.put('Input:Account', wrapped);
input.inputParams = params;
ConnectApi.EinsteinPromptTemplateGenerationsRepresentation result =
ConnectApi.EinsteinLlm.generateMessagesForPromptTemplate(
'Account_Summary', input);
return result.generations[0].text;
}
}Points to check in this code:
- The string
'Account_Summary'is the template's API name, not its label. - The key
Input:Accountmust match the input name in the template. In a Flex template, the input name you chose sets the key, such asInput:CaseorInput:MyContact. isPreview = falseasks for a real generation. Setting it totrueis the preview behavior, which returns the resolved prompt for inspection.- The call runs the active version. If no version is active, it fails.
Treat the call like any other callout-style operation. It adds latency, so avoid calling it in a loop over many records inside a trigger. Wrap it in try/catch, because the call can fail for permission, availability, or content reasons. Do not run it in a synchronous trigger path. Use Queueable Apex or a flow with an asynchronous path instead.
Invoking through the REST API and agents
The same generation capability is available through a REST endpoint under the Connect API, so external systems can call a template. The request names the template and passes the same input structure shown above. The caller authenticates as a Salesforce user, so field-level security and permission sets still apply.
Active templates can also back actions for Agentforce or Einstein agents. An agent can call a Record Summary or Flex template as an action, then use the result in its reply. For this to work, the template needs clear input names and a prompt that produces output suited to a conversation. The agent depends on the template output being predictable, so keep the output format stable between versions.
Where the output goes
Prompt Builder never stores generated text for you. The text goes to whatever called the template.
- In the UI, it is shown to the user, who saves or sends it.
- In Flow, it sits in a variable until a later element stores or uses it.
- In Apex or the REST API, it is a string in the response.
Generated text can include wording you did not intend, and it passes through the Einstein Trust Layer on the way out as it does on the way in. Plan how each caller handles an empty or error response. A flow with no fault path will fail silently from the user's point of view.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Template not listed in Flow or composer | No version is active, or the template type does not match the surface | Activate a version and check the type |
| Output has blank merge values | Running user lacks field access, or the field is empty | Check field-level security and add handling for empty values |
| Apex call fails with a not-found error | Wrong API name, or no active version | Use the API name and confirm activation |
| Input value is ignored | Input key does not match the template input name | Match `Input:<name>` exactly |
| Behavior changed overnight | A new version was activated | Check version history and reactivate the earlier version |
| Same flow gives different text each run | Model output varies between calls | Tighten the instructions and specify an output format |
Exercise
A record-triggered flow calls a Flex template named Case_Triage. After a colleague activates a new version of the template, the flow's output changes format and breaks a later step that parses it. Why did the flow change without anyone editing it, and what are two ways to prevent this?
Show answer
The flow refers to the template by name, so it always runs whichever version is active. Activating the new version changed what the flow gets. To prevent it, test new versions against sample records and check the output format before activating, and make the flow's later steps tolerant of format changes or add a Decision element that handles unexpected output. If the new version is already live, reactivate the previous version to roll back.
Exercise
An Apex method calling a Flex template with the key Input:Ticket returns an error, although the template previews correctly in Prompt Builder. The template's input is named Case. What is wrong, and how do you fix it?
Show answer
The key in inputParams must match the template's input name. The template defines Case, so the key must be Input:Case, not Input:Ticket. Change the key (or rename the template input, then save and activate a new version) so the two match.