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.
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:

