Einstein Prompt Grounding with Apex
This post shows how to integrate Apex with Salesforce’s Einstein Prompt Builder to include dynamic data in AI-generated sales email prompts. By defining an invocable Apex method with specific annotations and classes, you can pull product pricing from Salesforce records to ground AI prompts with accurate, contextual data. This enables more personalized, data-driven communication with leads, such as including standard product prices dynamically. Salesforce developers can apply this pattern to enhance Einstein GPT prompts using Apex for richer, real-time content inclusion.
- Create invocable Apex methods with @InvocableMethod and CapabilityType for Prompt Builder integration.
- Define inner Request and Response classes with exact property names to match prompt template bindings.
- Query Salesforce data (e.g., PricebookEntry) within Apex to dynamically ground prompts with live data.
- Deploy and register Apex methods carefully as capitalization errors require redeployment for recognition.
- Use Apex methods to enhance sales email templates with specific, context-aware pricing information.
Image generated by DALL-E 3 in response to a prompt from Bob Buzzard Introduction A couple of weeks ago I published my initial experience and thoughts on Einstein Prompt Builder , and one of my findings was that I couldn't get grounding with Apex to work, even using the example code in the help. Thanks to some help from Claudio Moraes from the Salesforce Partner team, that is no longer the case and I can pull information into my prompt via an Apex class. Example Scenario The prompt I've created is following up on a lead where we have a pretty good idea what product they are interested in. Using standard prompting I can get the user to select the product in questions, but I also want to include the standard price to give the lead an idea of what it might cost them. I can pull the PricebookEntries related list from the product into the prompt, but that is all or nothing, whereas I just want the a single entry from the standard price book.