Loading
Upcoming Mandatory Changes to Public Key Infrastructure (PKI)Read More
Salesforce Enforces New Security Requirements in Summer 2026Read More
Agentforce and Einstein Generative AI
Table of Contents
Select Filters

          No results
          No results
          Here are some search tips

          Check the spelling of your keywords.
          Use more general search terms.
          Select fewer filters to broaden your search.

          Search all of Salesforce Help
          Grounding with Apex Merge Fields

          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.

          Note
          Note

          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;
                  }
          Note
          Note If you change the inputs for a flow or Apex class that’s used as a resource in a prompt template, the prompt template no longer works and you can’t save new versions of the prompt template. See this known issue.

          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.

          Note
          Note If you open the Developer Console before you preview a prompt template that uses Apex, you can see debug statements in the Developer Console. For more information, see Working with Logs in the Developer Console and debug(msg) in the Apex Developer Guide.

          To use Apex in Prompt Builder, see:

           
          Loading
          Salesforce Help | Article