Add Apex Merge Fields to a Flex Prompt Template
Create an example Flex prompt template that uses Apex.
Required Editions
| Available in: Lightning Experience |
| Available in: Enterprise, Performance, and Unlimited Editions with the Einstein for Platform, or Einstein or Agentforce for Sales or Service add-on, or Agentforce Foundations |
| User Permissions Needed | |
|---|---|
| To create and manage prompt templates in Prompt Builder: | Prompt Template Manager permission set
OR Customize Application permission set |
Before adding the Apex fields, create an Apex class to use as a resource in the template. The class’s input variables must be a subset of the inputs to the template. The API name in the Apex class must match the API name in the template for each String input type and for any sobject input type used for more than one input in the template.
For instance, this example has two Account inputs in the prompt. To distinguish them, the object
API names must match the corresponding template inputs. The single Case input does not need
to match because there’s only one input of the Case type. You create the template
API Name fields and their input when you create the prompt template in
the UI.
public class ApexFlexTemplateExample1 {
@InvocableMethod
public static List<Response> getPrompt(List<Request> requests) {
Request input = requests[0];
List<Response> responses = new List<Response>();
Response output = new Response();
responses.add(output);
output.Prompt = 'generate a summary using the following info:';
// account_1 matches the API Name for the input
output.Prompt += '\nAccount 1: ' + input.account_1.Name;
output.Prompt += '\nAccount 2: ' + input.account_2.Name;
output.Prompt += '\nCase Number: ' + input.case_1.CaseNumber;
return responses;
}
// Type and API Name of all variables must match the template
public class Request {
@InvocableVariable(required=true)
public Account account_1;
@InvocableVariable(required=true)
public Account account_2;
@InvocableVariable(required=true)
public Case case_1;
}
public class Response {
@InvocableVariable
public String Prompt;
}
} In this example, we create a flex template that takes two accounts and one case.
- In Prompt Builder, click New Prompt Template.
- In the Prompt Template Type field, select Flex.
- In the Prompt Template Name field, enter Flex - Apex Example1.
- In the Define Resources section, enter the objects that the template uses.
- For the first row, enter account 1 in the Name field. In the Object field, select Account. Click Add.
- For the second row, enter account 2 in the Name field. In the Object field, select Account. Click Add.
- For the third row, enter case 1 in the Name field. In the Object field, select Case.
- Click Next.
- Click Save.
- Reload the template.
In Prompt Builder, add the ApexFlexTemplateExample1 class as a resource in the Flex template.
This example shows you how to send inputs for a Flex prompt template to an Apex class. The use case is simple but the concept is powerful. You can use Apex to process the records for your real-world use case.

