Grounding with Apex Merge Fields
You can use an Apex merge field in a prompt template to return data from a SOQL query or an external API. Apex is also effective if you want to generate well-formatted JSON or do programmatic data filtering.
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 |
To use Apex in Prompt Builder, you must create an Apex class that contains at least 1 method
annotated with InvocableMethod. For details, see the Apex Developer Guide.
If desired, the InvocableMethod annotation can specify a
CapabilityType that matches the prompt template type.
Example annotation:
@InvocableMethod(CapabilityType='PromptTemplateType://einstein_gpt__salesEmail').
This table lists the CapabilityType for each prompt template type.
Don’t specify a CapabilityType for the prompt template type
einstein_gpt__caseEmailDraft, used for Service Email Assistant. This
prompt template type uses Case as an input API name, so you should use another API name for
the input, like CaseObject.
| Prompt Template Type | CapabilityType |
|---|---|
| Sales Email |
PromptTemplateType://einstein_gpt__salesEmail
|
| Field Generation |
PromptTemplateType://einstein_gpt__fieldCompletion
|
| Record Prioritization |
PromptTemplateType://einstein_gpt__recordPrioritization
|
| Record Summary |
PromptTemplateType://einstein_gpt__recordSummary
|
The Flex capability has been retired, so it should no longer be used in Apex classes. We
recommend refactoring any Apex code that uses
CapabilityType=FlexTemplate://* so that it doesn’t use FlexTemplate. Remove
the CapabilityType attribute and save, and then verify that your prompt
template continues to work as expected with the Apex merge fields.
Method Input
The method’s input parameter must have a List<Request> type. The
Request class defines an InvocableVariable member for
each input defined by the prompt template type. All inputs are passed from the prompt
template into the invocable method. Only the first element in the list has data.
When present, the CapabilityType for a prompt template type acts as an
interface for the Request input class. Each CapabilityType
requires that the Request class defines specific
InvocableVariable members. For example, the field generation
CapabilityType requires that you define a RelatedEntity
member variable in Request. If there are additional objects defined in the
prompt template, an associated InvocableVariable member must be defined by using the exact
spelling and case sensitivity. In this example, the prompt template defined Account as the
object that the template is updating.
public class Request {
@InvocableVariable
public Account RelatedEntity;
}Method Output
The method output has a List<Response> type containing a single
string member named Prompt that’s annotated with
InvocableVariable. For details, see the Apex Developer Guide.
Example Apex Class
public class ApexPromptTemplateExample1 {
// At least one method must be annotated with InvocableMethod.
// No CapabilityType is specified (Flex is retired; for a typed template
// such as Sales Email or Field Generation, add e.g.
// @InvocableMethod(CapabilityType='PromptTemplateType://einstein_gpt__salesEmail')).
@InvocableMethod
public static List<Response> getPrompt(List<Request> requests) {
// Only the first element in the list has data.
Request input = requests[0];
List<Response> responses = new List<Response>();
Response output = new Response();
responses.add(output);
// Build the prompt string returned to the template.
output.Prompt = 'generate a summary using the following info:';
// account_1 matches the API Name of the input in the prompt template.
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;
}
// Method Input: List<Request>. Define one InvocableVariable per template input.
// The type and API Name of every variable must match the template exactly
// (spelling and case sensitivity).
public class Request {
@InvocableVariable(required=true)
public Account account_1;
@InvocableVariable(required=true)
public Account account_2;
@InvocableVariable(required=true)
public Case case_1;
}
// Method Output: List<Response> with a single String member named Prompt
// annotated with InvocableVariable.
public class Response {
@InvocableVariable
public String Prompt;
}
}Add Apex Resource to a Prompt Template
In the Prompt Template Workspace, add an Apex class to a prompt template by clicking the Resource field in the Prompt section and selecting Apex.
To use Apex in Prompt Builder, see:
