Step-by-Step Guide to Using Apex Merge Fields in Prompt Template

To enhance the effectiveness of prompt templates in Salesforce, it's important to make them context-aware, allowing AI models to deliver better results. One way to do this is by using merge fields to pull relevant data from your Salesforce org. This gives the AI the necessary context to generate more accurate and meaningful responses.

Salesforce Prompt Builder lets you use merge fields from different sources, including:

  1. Record Merge Fileds

  2. Related List Merge Fields

  3. Flow Merge Fields

  4. Apex Merge Fields

  5. Data Cloud DMO Merge Fields

  6. Record Snapshot Merge Fields


In this guide, we’ll focus on using Apex Merge Fields in a prompt template.

Business Use Case: Proactively Managing Opportunity Risks with AI

Imagine you're a sales rep managing multiple accounts, each with its own open opportunities and ongoing cases. Keeping track of everything can be challenging. Now, what if you had an AI assistant that could analyze these cases and opportunities, flagging any risks that might affect your deals?

That’s exactly what we aim to do here. By using Apex, we can filter down to only the open cases and opportunities related to a specific account. This data is then fed into the AI, prompting it to assess whether any open cases could negatively impact ongoing opportunities.

For instance, let's say you're about to close a deal with a customer for a large order of mobile generators. But there's also a high-priority case open from the same customer regarding issues with a generator they previously purchased. This could signal a potential risk - the customer might be hesitant to make another purchase if they're not satisfied with their existing product and/or your service.

Note: For this same business use case, we have also created a #Step-by-Step Guide to Using Flow Merge Fields in Prompt Template.

Where to Use Apex Merge Fields?

  1. Apex Merge Fields are ideal when you need to retrieve specific data based on complex logic

  2. When pulling data from external systems using Apex Callout

Step-by-Step Guide: Using Apex Merge Fields in Prompt Template

In this guide, we will walk through how to use Apex Merge Fields to analyze open Cases and Opportunities for an Account, focusing on how open cases may impact opportunities.

This process is simple and should take 20-30 minutes to configure and test. Here’s what the guide covers:

  1. Sign Up & Set Up Einstein Generative AI Trial Org

  2. Create Apex Classes to Get Open Cases & Opportunities

  3. Create Prompt Template

  4. Create Agent Custom Action

  5. Assign Custom Action to Einstein Copilot Agent

  6. Test Prompt Template with Einstein Copilot Agent


In the overall flow of Agentforce, the following diagram highlights the key components that we will cover in this guide.


Conclusion

Incorporating Apex Merge Fields into your prompt templates unlocks the potential for dynamic and precise AI-driven outputs. By leveraging the power of Apex, you can customize data inputs to align with complex business logic, ensuring the AI delivers highly relevant and actionable insights. This empowers your users with smarter automation and better decision-making capabilities.


Agentforce Advisory, Training & Implementation Services

If you’d like help going deeper with Agentforce beyond this guide, I offer the following:

  • Self-Paced Training – #Mastering Agentforce

    A structured, hands-on course covering Agentforce concepts, Prompt Templates, Agents, Actions, and real-world implementation patterns.

  • Live Online & Onsite Agentforce Training

    Instructor-led training for Salesforce teams, partners, and customer organizations, tailored to real use cases and practical scenarios.

  • Agentforce Advisory & Implementation Services

    Strategic advisory and hands-on implementation support to help you design, build, and scale Agentforce solutions with confidence.

For training, advisory, or implementation discussions, please reach out via the Contact page, where you can submit a form or schedule a conversation.


References & Useful URLs



Tried this out? Tell us how it went in the comments 👇