Apex Aide apexaide

Einstein Prompt Templates in Apex - the Sales Coach

By Bob Buzzard· Bob Buzzard Blog· ·Intermediate ·Developer ·5 min read
Summary

This content details how to integrate Salesforce Einstein prompt templates into custom Apex code to deliver AI-driven coaching advice for sales reps on opportunity management. It guides developers on preparing prompt inputs with ConnectAPI, calling the Large Language Model, handling responses, and displaying results in a Lightning Web Component. The approach addresses cost control by triggering AI interaction only when a user selects a specific tab. Salesforce teams can build similar interactive AI-powered helpers embedded in record pages to enhance sales guidance within their environments.

Takeaways
  • Use ConnectAPI.EinsteinPromptTemplateGenerationsInput to prepare prompt inputs in Apex.
  • Send prompts to LLM via ConnectAPI.EinsteinLLM.generateMessagesForPromptTemplate with a prompt template Id.
  • Extract AI responses from ConnectAPI.EinsteinLLMGenerationItemOutput generations list.
  • Invoke Apex calls imperatively in LWC to handle non-cacheable ConnectAPI methods.
  • Limit AI calls by placing the interaction inside a non-default tab to control token usage.

Image by DALL-E 3 based on a prompt from Bob Buzzard Introduction Thus far in my journey with Generative AI in Salesforce I've been focused on using prompt templates out of the box - sales emails, field generation, or in custom copilot actions. Salesforce isn't the boss of me though, so I'm also keen to figure out how to include prompts into my own applications. The Use Case I want a way for sales reps to get advice on what they should do with an opportunity - is it worth proceeding with, what does it make sense to do next, that kind of thing. I don't want this happening every time I load the page, as this will consume tokens and cost money, so I'm going to make it available as an LWC inside a tab that isn't the default on the opportunity page: The Coach tab contains my LWC, but the user has to select the tab to receive the coaching.

Apex