top of page

How to Call Agentforce Prompt Builder from Apex and Flow

In the fast-paced world of software development, connecting different systems is both a challenge and an opportunity. One important integration is calling the Agentforce Prompt Builder from Apex and Flow. This skill is vital for any Salesforce developer eager to improve their applications. In this guide, we'll walk you through the process step by step, ensuring clarity and comprehension.


Understanding Agentforce Prompt Builder


Before jumping into code, it's important to understand what the Agentforce Prompt Builder is and how it can enhance your Salesforce applications. The Agentforce Prompt Builder enables you to create customized prompts for customer interactions.


Imagine a scenario where a customer needs help with a product. Instead of a generic response, the Prompt Builder can deliver tailored prompts based on customer inquiries. This feature is especially beneficial for developing chatbots or automated customer service agents. According to a study by Gartner, 70% of customer interactions will involve emerging technologies like chatbots by 2022. Leveraging tools like Agentforce can help you stay ahead.


Create a Prompt Template by navigating to Setup, searching for Prompt Builder, and clicking New.



There are different types of Template Type. Choose which suits you most. Below are the detail of each type.
There are different types of Template Type. Choose which suits you most. Below are the detail of each type.

1. Einstein AI-Generated Search Answers

  • Purpose: Generates answers by interpreting natural language questions using indexed content (like Knowledge articles or records).

  • Use Case: Great for building a chatbot or assistant that provides concise answers based on data in your org.


2. Extract Product Mentions

  • Purpose: Identifies and extracts product names from text.

  • Use Case: Useful in support cases, emails, or chats where users mention products, allowing automation based on what's referenced.


3. Field Generation

  • Purpose: Generates or suggests field values (e.g., auto-fill case descriptions or classifications).

  • Use Case: Automate data entry or suggest field inputs based on context from records or messages.


4. Flex

  • Purpose: A general-purpose prompt template that gives you maximum control over prompt instructions and formatting.

  • Use Case: Ideal for advanced use cases like JSON generation, dynamic instructions, or AI-driven logic (e.g., setup automation).


5. Knowledge Answers

  • Purpose: Specifically tuned to search and summarize Salesforce Knowledge Base articles.

  • Use Case: Used in bots or support to answer FAQs by pulling directly from your Knowledge records.


6. Record Summary

  • Purpose: Summarizes a record’s key details into a brief, natural language format.

  • Use Case: Helpful for agents who need a quick summary of a case, lead, or opportunity.


Fill the rest of the fields and then click "Next". For this example, I am using the "Einstein AI-Generated Search Answers" type.


This is sample prompt, you can also write your own. Don't forget to activate this.
This is sample prompt, you can also write your own. Don't forget to activate this.

Now create an auto-launched flow, and add an action. It will open a list on the right-hand side. Search for Prompt Builder and click on it.

You will see all the prompt builder templates created. Select the one you created in the previous step.


Create three resources: User_Input and input_chunks as 'input', and Response as 'output'.


The chunks field is to provide the context. For example, your conversation spans multiple messages, so chunks will contain the old messages, allowing the prompt builder to effectively answer the question.


Populate your action as below.

Now save and activate your flow. Next, we will create an Apex class to call this flow, which will call our Prompt Builder.


Now paste the code below in any class or in an anonymous window.


Run the execution and verify the output. The output will be in JSON format as shown below.

{

"answer": "Subject: Follow-Up on Mulesoft Developer Inquiry\n\nDear Saurabh Singh,\n\nI hope this message finds you well. I wanted to follow up regarding your interest in finding a Mulesoft Developer, which I came across through Instagram. At CloudTech Solutions, we have a range of talented developers who could meet your needs at Maxify Digital.\n\nI would appreciate the opportunity to discuss your specific requirements and how we can assist you. Please let me know a convenient time for a call, or feel free to suggest an alternative.\n\nThank you for considering us. I look forward to your response.\n\nBest regards,\n\n[Your Name] \nSales Representative \nCloudTech Solutions \n[Your Contact Information]",

"sources": ""

}


Deserialize the response, and you will have the email content. This way, you can utilize the Prompt Builder and use the Einstein Co-Pilot functionality directly from your code.


Like and Comment if you like this blog.

 
 
 

Subscribe to SFDC BLOGS

©2019 by SFDC Blogs.

bottom of page