---
schema_version: "1"
id: c111412c6f78188664ded2862518ca61e1f078ea3ddae8190dfd383fad17b626
title: Add Apex Merge Fields to a Flex Prompt Template
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/ai/264-0-0/prompt_builder_add_apex_flex.md
canonical_url: https://help.salesforce.com/s/articleView?id=ai.prompt_builder_add_apex_flex.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: d60256e9873ec9baae3d4b65b943fc83780607eab6843ed27d337ccc923366f4
description: Create an example Flex prompt template that uses Apex.
products:
  - Agentforce
  - Cross Cloud
release: "264"
---
# Add Apex Merge Fields to a Flex Prompt Template

Create an example Flex prompt template that uses Apex.

### Required Editions

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition"><colgroup><col></colgroup><tbody><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: Lightning Experience</td></tr><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: <strong>Enterprise</strong>, <strong>Performance</strong>, and <strong>Unlimited</strong> Editions with the Einstein for Platform, or Einstein or Agentforce for Sales or Service add-on, or Agentforce Foundations</td></tr></tbody></table>

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small permission">
        <colgroup>
          <col style="width:50%">
          <col style="width:50%">
        </colgroup>
        <thead class="slds-text-title_caps ">
          <tr class="">
            <th scope="colgroup" class="slds-cell-wrap" colspan="2" style="text-align:center;vertical-align:top;" id="d205298e63">User Permissions Needed</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d205298e63 ">To create and manage prompt templates in Prompt Builder:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d205298e63 ">
              <p>Prompt Template Manager permission set</p>
              <ul>
                <li>Manage Prompt Templates</li>

                <li>Execute Prompt Templates</li>

              </ul>
              <p>OR</p>
              <p style="margin-bottom:0;">Customize Application permission set</p>
            </td>

          </tr>
        </tbody>
      </table>

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.

```java
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](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-ai-generative_ai-264-0-0-production-enus/f8ef6498-d7b2-44e3-9b34-ffd5a850aae9/images/icon_note.png)

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.