Step-by-Step Guide to Calling Prompt Template from Apex

This guide is part of our series on “Mastering Agentforce”.

To explore the complete series, please click here.

Generative AI is transforming the way businesses operate, and Agentforce is at the forefront of this revolution. Prompt Template (one of the core components of Agentforce), when paired with Apex (Salesforce’s powerful programming language), creates limitless possibilities for automation, efficiency, and innovation.

Let’s explore how this dynamic duo can revolutionize your workflows, drive productivity and streamline complex processes.

What is a Prompt Template?

Prompt Templates allow you to define AI prompts with placeholders (merge fields) that can be populated with real-time data, ensuring your prompts are always contextually relevant.

Prompt Templates can be called from various places in Salesforce—Flows, Apex, APIs or even directly within user interfaces—allowing companies to harness AI capabilities seamlessly within their daily workflows.

What is Apex?

Apex is Salesforce’s own powerful programming language. With Apex, you can customize Salesforce to match your unique business needs, far beyond the capabilities of standard point-and-click configurations.

The Dynamic Duo: Prompt Templates + Apex

By integrating Prompt Templates with Apex, you can create truly intelligent automations. Apex can call Prompt Templates, pass dynamic data to them, and then handle the AI-generated responses efficiently.

This integration allows organizations to:

  • Automate complex repetitive tasks with precision
  • Process large amounts of data intelligently
  • Enable smarter workflows without requiring user intervention

Let’s explore this synergy through a real-world use case.

Real-World Use Case: Extracting Key Details from Case Description

The Challenge:

Support agents often receive case descriptions filled with unstructured information. Extracting critical details like Customer Name, Email Address, and Phone Number manually is both time-consuming and prone to errors.

The Solution:

  1. Create a Prompt Template:
    • Define a template to analyze the case description and extract the name, email & phone using Generative AI.
    • The template includes explicit instructions to return the response in a structured JSON format, so that it can be processed by Apex.
  2. Create a Trigger:
    • Write an Apex Trigger on the Case object to automatically call the Prompt Template whenever a new case is created.
  3. Invoke Prompt Template from Apex:
    • Use Apex to call the Prompt Template and pass the Case Description as input.
  4. Process the AI Output:
    • The Prompt Template sends the prompt, including the merged data, to the LLM (Large Language Model).
    • The LLM generates a response in JSON format and returns it to Apex.
    • Apex processes the JSON data, extracts the details (Name, Email, Phone), and saves them to the appropriate fields on the Case object in Salesforce.

The Outcome:

Reduced manual effort for agents
Faster resolution times
Improved data accuracy

This integration bridges the gap between unstructured inputs and actionable data, all within Salesforce.

Here’s a visual representation of what we’ll be building in this guide:

Business Use Case for Calling Prompt Template from Flow
Business Use Case for Calling Prompt Template from Flow

Step-by-Step Guide: Calling Prompt Template from Apex

In this guide, we’ll walk you through creating a Prompt Template, an Apex Trigger, and the necessary Apex Code to automatically extract and save the Name, Email, and Phone Number from a Case Description directly onto the Case Record.

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

  1. Sign Up & Set Up Agentforce Trial Org
  2. Create a Sample Case Record
  3. Create Prompt Template
  4. Create Apex Class & Trigger
  5. Test Apex with Prompt Template

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

Agentforce Flow Overview - Calling Prompt Template from Apex

NOTE: Certain sections of the guide will appear as locked in the free preview. You can download the unlocked version of the guide in PDF format by subscribing to our “All Access” Pass through the link below.

Not an “All Access” Pass Member Yet?

Get Download Access to this & 150+ More Step-by-Step Guides with “All Access” Pass. A simple and single plan to access our entire library of courses, guides, workshops & masterclasses on Salesforce.

Unlock the Power of Salesforce Generative AI: Master Agentforce!

Discover how to leverage Salesforce’s cutting-edge Generative AI tools to streamline your workflows and boost productivity.

Our comprehensive guides and hands-on workshops will take you from beginner to expert in no time. Learn the ins and outs of Agentforce, Agents, Topics, Actions and Prompt Builder with step-by-step tutorials, best practices, and real-world examples.

We also offer live onsite/online training for Salesforce partners, as well as consulting and implementation services for Salesforce customers.

More Real-World Use Cases for Prompt Templates + Apex

  • Automated Follow-Up Emails
    • Scenario: After a sales call, summarize key discussion points and send a follow-up email.
    • How it works: Apex fetches call notes, passes them to a Prompt Template, and generates a personalized follow-up email draft.
  • Opportunity Insights
    • Scenario: Summarize critical details from opportunity notes to help sales reps prioritize deals.
    • How it works: Apex analyzes opportunity records using a Prompt Template, generates insights, and saves them as actionable points.
  • Content Generation
    • Scenario: Generate content for various Social Media platforms like LinkedIn, X (Twitter), Instagram etc.
    • How it works: Apex passes the content topic and a brief description to prompt template. The response returned by the prompte template is then displayed in a LWC component for the user to review, update and save.
  • Predictive Maintenance
    • Scenario: Analyze sensor data from connected devices to predict potential issues and trigger proactive maintenance workflows.
    • How it works: Apex collects sensor data from connected devices, passes it to a Prompt Template, and generates predictive insights. These insights can then be used to trigger automated maintenance workflows, create service cases, or notify relevant teams for timely action.

Conclusion

Salesforce’s Prompt Template, when paired with Apex, creates a powerful synergy for intelligent automation and unlocks immense potential for innovation in Salesforce. This combination allows businesses to:

  • Automate complex tasks with precision
  • Enhance productivity and efficiency
  • Deliver smarter, data-driven decisions

If you haven’t tried integrating them yet, now is the time to explore!

References & Useful URLs

Scroll to Top