Sub topic 12
Calling the Same Template from Flow and Apex
A prompt template is not tied to an agent. Once it is saved and active, you can call it from Flow or Apex, so one template can back an agent action, a screen flow button, and a batch job. This chapter shows both routes and the errors that come up most.
This chapter assumes you have already built and activated a template such as Summarize_Case, and that you know basic Flow and Apex. You also need the template's input resources, covered in the earlier chapter on inputs and merge fields.
Why reuse the template
Reuse keeps one prompt as the source of truth. If you tighten the rules in the template, the agent, the flow, and the Apex code all change together. Only the active version runs, so version control and activation (covered earlier) apply to every caller.
Calling from Flow
Flow has a core action for prompt templates. The steps in Flow Builder are:
- Create a record-triggered, screen, or autolaunched flow.
- Add an Action element and search for your template by name. Templates appear under the Prompt Template category.
- Map each input. For
Summarize_Case, set theInput:Casevalue to the record variable that holds the Case. - Store the Prompt Response output in a text variable.
- Use the variable in the next element, for example to show it on a screen or to write it to a field.
The input you pass must match the type the template expects. If the template takes a Case record, pass a Case record variable, not a text Id.
Calling from Apex
Apex uses the Connect API. The code below calls Summarize_Case and passes a Case Id.
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(
'Summarize_Case', input);
String summary = result.generations[0].text;Three details matter here:
- The first argument is the template's API name, not its label.
- The key in
inputParamsisInput:followed by the input name defined in the template. A mismatch gives an error or an empty merge field. caseIdmust already be declared in your code, for example as a method parameter.
Flow compared with Apex
| Need | Better fit |
|---|---|
| Admin maintains the logic, no code | Flow |
| Show the result on a screen | Screen flow |
| Process many records with custom logic | Apex |
| Use the result in a larger Apex transaction | Apex |
Common errors
- Template not found. The API name is wrong, or the template has no active version.
- Wrong input shape. Flow expects a record variable, and Apex expects the
idmap as shown above. Passing the wrong type leaves merge fields blank. - Missing access. The running user needs access to Prompt Builder templates and to the records and fields the template reads. A user without field access gets a summary with gaps.
- Calls inside loops. Each call is a model request. Calling it once per record inside a loop can hit limits and add latency, so bound the number of calls.
Exercise
An Apex call to Summarize_Case returns a summary where every field says "unknown". The template works in Prompt Builder preview. Name two likely causes in the calling code or its setup.
Show answer
The inputParams key may not match the template's input name (it must be Input:Case), or the Id passed in caseId may be wrong or point to a record the running user cannot read. Missing field access for the running user is another valid cause.