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

  • Manage Prompt Templates
  • Execute Prompt Templates

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;
    }
}   
Note
Note The output variable in the Response class should always be named "Prompt" in order for it to be discovered by the Resource Picker in the Prompt Builder UI.

In this example, we create a flex template that takes two accounts and one case.

  1. In Prompt Builder, click New Prompt Template.
  2. In the Prompt Template Type field, select Flex.
  3. In the Prompt Template Name field, enter Flex - Apex Example1.
  4. In the Define Resources section, enter the objects that the template uses.
    1. For the first row, enter account 1 in the Name field. In the Object field, select Account. Click Add.
    2. For the second row, enter account 2 in the Name field. In the Object field, select Account. Click Add.
    3. For the third row, enter case 1 in the Name field. In the Object field, select Case.
  5. Click Next.
  6. Click Save.
  7. 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.