Sub topic 12
Activating templates and invoking them from Salesforce
A template you have previewed and tuned does nothing for users until you activate a version of it and connect it to a place where it runs. This chapter covers that last step. It assumes you can build the four template types and use merge fields. It also assumes you know basic Flow and Apex.
Activating a version
Each template has versions, and each version has a status. A new version starts as a draft. Activating a version makes it the one that runs.
- Only one version of a template is active at a time. Activating another version replaces the current one.
- An active version is locked. To change it, clone it into a new draft, edit the draft, preview it, and activate it.
- Callers that refer to the template by name get the active version automatically, so you can roll out a change without editing the flow, page, or code.
- If a new version behaves worse, activate the earlier version again.
Where each type appears
The type of template mostly decides where users meet it.
| Template type | Typical place the output appears |
|---|---|
| Sales Email | The email composer, as a generated draft the user can edit before sending |
| Field Generation | A text field on a record page, filled in for the user to review and save |
| Record Summary | A Lightning record page, where the summary is shown on the record |
| Flex | Wherever you call it from: a flow, Apex, or an API call |
Object-bound templates are usually placed through Lightning App Builder and field settings. Flex templates have no built-in location, so you invoke them yourself.
Permissions
Users need access before they can run a template. Salesforce provides the Prompt Template User permission set for running templates and the Prompt Template Manager permission set for building them. Users also need access to the objects and fields that the merge fields read. If a user lacks field access, that value is not available to the prompt.
Invoking from Flow
In Flow Builder, add an action and choose your template from the prompt template actions. Pass the input, such as a Case record, and the action returns the generated text in an output variable. You can then do what you need with that text: save it to a field, post it to Chatter, or include it in an email. This works for record-triggered, screen, and scheduled flows. A scheduled flow suits batch work, but each call uses model capacity, so scope the records it processes.
Invoking from Apex
Apex can call a template through the Connect API. The following example runs a Flex template named Case_Summary whose input is a Case. The key Input:Case must match the input name defined in the template.
public with sharing class CaseSummaryService {
public static String summarize(Id caseId) {
ConnectApi.EinsteinPromptTemplateGenerationsInput input =
new ConnectApi.EinsteinPromptTemplateGenerationsInput();
input.isPreview = false;
input.additionalConfig = new ConnectApi.EinsteinLlmAdditionalConfigInput();
input.additionalConfig.applicationName = 'PromptBuilderPreview';
ConnectApi.WrappedValue caseValue = new ConnectApi.WrappedValue();
caseValue.value = new Map<String, String>{ 'id' => caseId };
input.inputParams = new Map<String, ConnectApi.WrappedValue>{
'Input:Case' => caseValue
};
ConnectApi.EinsteinPromptTemplateGenerationsRepresentation result =
ConnectApi.EinsteinLLM.generateMessagesForPromptTemplate('Case_Summary', input);
return result.generations[0].text;
}
}The same template can also be called through the Connect REST API, and from agent actions where your org has agents enabled.
Common errors
- Template not found or not callable: no version is active, or the API name in the call does not match.
- Missing or empty values in the output: the running user cannot see a field, or the record has no data. Tell the prompt how to handle empty values, as in the earlier Field Generation example.
- Input mismatch: the input name in the call, such as
Input:Case, differs from the name in the template. - Edits that seem to have no effect: you changed a draft but did not activate it.
Every call goes through the Einstein Trust Layer, whichever way you invoke it.
Exercise
A Flex template is active, and a flow calls it. You edit the template's prompt text and want the flow to use the new wording. What do you do, and do you need to change the flow?
Show answer
Clone the active version, edit the new draft, preview it, and activate it. The flow refers to the template by name, so it needs no change and uses the newly active version.
Exercise
Which template types are placed through a record page or the email composer, and which is invoked from a flow or Apex?
Show answer
Sales Email, Field Generation, and Record Summary templates are bound to an object and appear in the email composer or on record pages. Flex templates are invoked from a flow, Apex, or an API call.