Add Apex Merge Fields to a Field Generation Prompt Template
Create an example field generation 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 |
Say you want to provide up-to-the-minute summaries of customer cases for a given customer to your company’s sales team before they make customer calls. We used the same use case in the flow section.
This time, you want to create a field generation prompt template that integrates an Apex class that obtains case data for an LLM to summarize.
Here’s the Apex class that we access as a resource in a prompt template.
public class OpenCasesPrompt {
@InvocableMethod(label='Open Cases'
description='Find Cases for an Account'
CapabilityType='PromptTemplateType://einstein_gpt__fieldCompletion')
public static List<Response> getCasesPrompt(List<Request> requests) {
// Validate the expected number of requests as an input
if (requests.size() != 1)
throw new ListException('The requests list must contain one entry only');
Account a = requests[0].RelatedEntity;
ID searchAcctId = a.Id;
List<Case> cases =
[SELECT Id, Subject, Description
FROM Case
WHERE AccountId = :searchAcctId AND Status != 'Closed'
WITH USER_MODE];
string responseData = null;
if(cases.isEmpty()) {
responseData = 'There are no open cases.';
} else {
for(Case c : cases) {
responseData =
(responseData != null) ? responseData + '\n' : '';
responseData += String.format('Case details: {0}, {1}.',
new List<Object>{c.Subject, c.Description});
}
}
List<Response> responses = new List<Response>();
Response res = new Response();
res.Prompt = responseData;
responses.add(res);
return responses;
}
public class Request {
@InvocableVariable(required=true)
public Account RelatedEntity;
}
public class Response {
@InvocableVariable
public String Prompt;
}
}The requirements for the Apex class are explained in Grounding with Apex Merge Fields.
Test coverage is important so let's add a test class. The test class simulates data sent from Prompt Builder. Two test cases are provided: one for when events are found, and one for when there are no events.
@IsTest
public class OpenCasesPrompt_Test {
@IsTest
public static void findMatchingCasesForAccount() {
Account a = new Account(Name='Test Account');
insert a;
Case c1 = new Case(Subject='Test Case', Description='Important Case',
Status='New', AccountId=a.id);
Case c2 = new Case(Subject='Test Case 2',
Description='Case Description', Status='In-progress',
AccountId=a.id);
Case c3 = new Case(Subject='Test Case Closed',
Description='Closed Case', Status='Closed', AccountId=a.id);
insert new List<Case>{c1, c2, c3};
List<OpenCasesPrompt.Request> requestsInput
= new List<OpenCasesPrompt.Request>();
OpenCasesPrompt.Request request = new OpenCasesPrompt.Request();
request.RelatedEntity = a;
requestsInput.add(request);
Test.startTest();
List<OpenCasesPrompt.Response> responses =
OpenCasesPrompt.getCasesPrompt(requestsInput);
Test.stopTest();
Assert.areEqual(1, responses.size());
OpenCasesPrompt.Response response = responses[0];
Assert.areEqual('Case details: Test Case, Important Case.\n' +
'Case details: Test Case 2, Case Description.',
response.Prompt);
}
@IsTest
public static void noCasesForAccountResponse() {
List<OpenCasesPrompt.Request> requestsInput
= new List<OpenCasesPrompt.Request>();
OpenCasesPrompt.Request request = new OpenCasesPrompt.Request();
request.RelatedEntity = new Account();
requestsInput.add(request);
List<OpenCasesPrompt.Response> responses =
OpenCasesPrompt.getCasesPrompt(requestsInput);
Assert.areEqual(1, responses.size());
OpenCasesPrompt.Response response = responses[0];
Assert.areEqual('There are no open cases.', response.Prompt);
}
}
Now that the Apex class is ready, let’s call it from Prompt Builder. Before we can do that, we must create a custom field called Open Case Summary on the Account object. The custom field should have a data type of Text Area (Long). See Create Custom Fields. Log out and log in again so that you can see the new custom field in Prompt Builder.
In Prompt Builder, create a new prompt template.
- For Prompt Template Type, select Field Generation.
- For the Prompt Template Name, enter Apex: Summarize Open Cases.
- For the Object, select Account.
- For the Object Field, select Open Case Summary.
- Leave the Description field blank.
- Click Create, and you’re redirected to the Prompt Template Workspace.
- In the Example Prompt Template Library, find the field generation prompt template: Summarize open cases for an account. Copy and paste this template into the Prompt Template Workspace.
-
Before you can run this template in Prompt Builder, in the Prompt section,
click Insert Resource. The search field appears the first
time you click this option.
-
Delete the
<account.Id__Merge_Field>placeholder. - Click the Resource search bar, and select Account | Account ID .
- Delete the <Get_Open_Cases_For_Account__Prompt__Flow> placeholder.
- Click the Resource search bar, and select Apex.
- To add the OpenCasesPrompt Apex class to your template, select Open Cases. Open Cases is the label attribute that you set in the @InvocableMethod annotation in the OpenCasesPrompt Apex class.
-
Delete the

