Sunday, 16 November 2025

Grounding Prompt Templates with Apex Merge Fields

 Hi,

You can include an Apex merge field in a prompt template to surface data retrieved from a SOQL query or an external API. Apex is also useful when you need to generate structured JSON or apply programmatic data filtering.

To use Apex within Prompt Builder, create an Apex class that contains at least one method annotated with @InvocableMethod.

If needed, you can specify a CapabilityType in the annotation so it aligns with the prompt template’s type.

Example:
@InvocableMethod(CapabilityType='PromptTemplateType://einstein_gpt__salesEmail')


Prompt Template TypeCapabilityType
Sales EmailPromptTemplateType://einstein_gpt__salesEmail
Field GenerationPromptTemplateType://einstein_gpt__fieldCompletion
Record PrioritizationPromptTemplateType://einstein_gpt__recordPrioritization
Record SummaryPromptTemplateType://einstein_gpt__recordSummary


The Flex capability has been retired, so it should no longer be used in Apex classes. Salesforce recommends refactoring any Apex code that specifies CapabilityType=FlexTemplate://* to remove the FlexTemplate reference entirely. Simply delete the CapabilityType attribute, save the class, and then confirm that your prompt template still functions correctly with its Apex merge fields.

Notes:

  • Do not specify a CapabilityType for the prompt template type einstein_gpt__caseEmailDraft, used by Service Email Assistant. This template type uses Case as its input API name, so choose a different API name for the input—such as CaseObject.

  • If you change the inputs for a Flow or Apex class used as a resource in a prompt template, the template will no longer function, and you won’t be able to save new versions.


Method Input Requirements

The method’s input parameter must be a List<Request>. The Request class must define an InvocableVariable for each input required by the prompt template type. All inputs from the prompt template are passed into the invocable method, and only the first list element contains data.

When a prompt template type includes a CapabilityType, it effectively acts as an interface for the Request class. Each CapabilityType requires that the Request class include specific InvocableVariable fields.
For example, the field generation CapabilityType requires a RelatedEntity member. If the prompt template includes additional objects, the Request class must define matching InvocableVariable members using exact spelling and case sensitivity.
In this example, the prompt template specifies Account as the object being updated, so the Request class must declare a corresponding Account variable.


public class Request {
    @InvocableVariable
    public Account RelatedEntity;
}



Method Output

The method must return a List<Response>. The Response class contains a single string field named Prompt, which must be annotated with @InvocableVariable.

Adding an Apex Resource to a Prompt Template

In the Prompt Template Workspace, you can add an Apex class to a prompt template by selecting Insert Resource in the Prompt section and choosing Apex from the list.









Reference:

https://help.salesforce.com/s/articleView?id=ai.prompt_builder_ground_apex.htm&type=5

https://help.salesforce.com/s/articleView?id=ai.prompt_builder_add_apex_flex.htm&type=5

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm



Grounding Prompt Templates with Apex Merge Fields

 Hi, You can include an Apex merge field in a prompt template to surface data retrieved from a SOQL query or an external API. Apex is also u...