Introduction
Salesforce Agentforce is an innovative AI-driven platform designed to create autonomous, proactive agents that support both employees and customers in different business functions. These agents are equipped to handle tasks on their own by utilising trusted data, advanced reasoning, and predefined actions to provide accurate and context-aware responses.
In this blog, we will explore how to build an agent using Apex and demonstrate how you can enhance Agentforce’s capabilities by integrating Apex for customised solutions and advanced functionalities.
What is Salesforce Agentforce?
Agentforce is an AI-powered solution that allows organisations to create and deploy autonomous agents that can manage tasks across sales, service, marketing, and commerce. These agents leverage large language models (LLMs) and Salesforce’s Atlas Reasoning Engine to analyze data, make decisions, and perform tasks independently. Operating 24/7 across various channels like websites, Slack, WhatsApp, and CRM systems, they provide seamless automation and support.
Key Features of Agentforce:
- Autonomous Task Execution: Agents can handle complex, multi-step tasks like resolving customer cases, qualifying sales leads, and optimizing marketing campaigns. They operate independently within predefined guardrails, ensuring accuracy and relevance.
- Integration with Salesforce Ecosystem:
Agentforce integrates seamlessly with Salesforce tools like Data Cloud, Slack, MuleSoft, and Flow Builder. This allows agents to access and act on real-time data from multiple sources, ensuring context-aware responses.
- Customizable Agents:
Using Agent Builder, a low-code tool, organizations can create agents tailored to specific roles, industries, or use cases. Agents can be equipped with custom topics, actions, and natural language instructions
- Proactive and Reactive Capabilities:
Agents can proactively address customer needs based on data triggers or reactively respond to queries. For example, they can escalate complex issues to human agents while providing a summary of the interaction - Trust and Security:
The Einstein Trust Layer ensures data security and compliance. Agents operate within strict guardrails, preventing unauthorized actions and ensuring responsible AI usage.
How Agentforce Works
- Data Integration:
Agents are connected to enterprise data sources, including CRM data, knowledge articles, and external systems through Data Cloud. This integration ensures they have access to the latest and most relevant information. - Reasoning and Decision-Making:
Powered by the Atlas Reasoning Engine, agents analyze queries, retrieve pertinent data, and develop action plans. This engine mimics human reasoning to provide precise and context-aware responses. - Action Execution:
Agents can carry out predefined actions, such as running flows, updating records, or sending messages, triggered by specific instructions or data changes. - Multi-Channel Deployment:
Agents can function across various platforms, including websites, messaging apps, and Slack. For example, employees can engage with agents directly in Slack using natural language commands.
Implementing a Sales Agent Using Apex Actions for Account Operations:
Now, let’s implement a sales Agent that leverages Apex actions to streamline sales operations on the Account object.
Step 1: Configure Your Org for Agentforce:
Agentforce, Salesforce’s Einstein Generative AI platform, is not yet accessible in Developer Edition orgs. However, you can sign up for a trial org via one of the Trailhead modules.
Go to this link
https://trailhead.salesforce.com/content/learn/projects/quick-start-build-your-first-agent-with-agentforce/configure-an-agentforce-service-agent?trail_id=get-ready-for-agentforce
there you are able to create the Playground by clicking on the “Get Started by Free” button.
Step 2: Enable Einstein Setup and Agents
Enable Required Org Features by following the steps.
- Click the setup icon and select Setup. This will open the Setup page in a new tab.
- In the Setup Quick Find search bar, type and select Einstein Setup. Toggle the Turn on Einstein switch to ensure Einstein is enabled.
- Refresh your browser to reload the Setup page.
- In the Setup Quick Find, search for and select Agents. And Toggle the Agentforce switch to On.
Step 3: Creating an Agent
Now that the basic setup for Agentforce is complete, we will proceed to create an Agent and configure actions step by step.
- In the Setup, navigate to Agents, and you will find the New Agent button to begin creating a new agent. It will be redirected to open Agent Builder.
- Select Agentforce Service Agent as the selection type and proceed to the next step.
- In the second step, you’ll see the Review Topics section where you can select default standard topics like Case Management, Account Management, and so on. These are optional, but you will also have the option to add custom topics if needed. For now, just selecting the General FAQ topic will add our own Topic.
What are Topics:
A topic is a category of tasks an agent performs. Adding topics helps the agent know how to respond and what tools to use. Topics include actions and instructions (guidelines for decision-making). They define the agent’s capabilities and improve response accuracy.
Once your Agent is created, you can customize or create new topics in the Agent Builder. - In the third step, you’ll define the settings for your agent, including the name, description, role, company, and most importantly, the agent itself. For this, I’m selecting the EinsteinServiceAgent user with the profile Custom Einstein Agent User. This profile must be added when creating a new class, as it is mandatory.
- After clicking Next, you’ll have the option to select or turn on Data Cloud. You can ignore this for now and click the Create button. The agent will then be created.
Step 4: Creating Topics
- Once the Agent is created, you’ll need to create a Topic. You can either select an existing topic by clicking on “Add from Asset Library” or create a new one by clicking the New Topic button.
- A new popup will appear where you need to fill in the topic details, such as Topic Label, Classification, Description, Scope, and Instructions. All of these fields are mandatory, and you must complete them to ensure accurate results.
- Adding Instructions to make decisions on how to use the actions within a topic for different use cases. For example, you can instruct the agent to collect clarifying information before executing an action or to update account information when necessary. By clicking on the Add Instructions button you will be able to add more instructors.
I’m adding two instructions to retrieve and update the account phone number as follows. Instructions are crucial for a better user experience, so it’s recommended to include more instructions.
Instruction 1: If the user asks about the phone number of the account, run the ‘Account Phone’ action and provide the phone number of the account. If the account name is not a string, inform the user to provide the account name as a string.
Instruction 2: If the user asks to update the phone number of the account, run the ‘Update Account Phone’ action and prompt the user to provide the account name and the new phone number. These two details are mandatory.
After adding the instructions, click the “Next” button to add the actions. However, before proceeding, let’s first create the action, and then we can come back to this step to complete the process.
Step 5: Creating Apex method and Actions
- Apex Method 1: (Account Phone) I have created two classes. One is AccountAgentController, which contains the getPhone() method to retrieve the phone number filtered by the account name provided by the user during the agent conversation. This method should be annotated with @InvocableMethod and should have the label “Account Phone,” as this is required to use the method while creating the Apex action. The description is optional and can store the instructions for the action. This Apex method should accept only a list as a parameter.
public with sharing class AccountAgentController {
@InvocableMethod(
label=‘Account phone’
description=‘Retuns the Phone of the Account, when user ask for it, for examples user ask what is the Phone of Dickenson plc’)
public static List<String> getPhone(List<String> accountName){
Account acc = [Select id, Phone from Account where name =: accountName[0]];
return new List<String>{acc.Phone};
}
}Make sure to add the AccountAgentController class to the Custom Einstein Agent User profile to grant access to this class.
- Apex method2 (Update Account Phone): The second class is UpdateAccountPhoneController, which contains the updatePhone() method. This method takes a list of multiple inputs, so we created a wrapper class to handle the account name and phone number inputs. It will retrieve the account details using the account name and update the phone number with the new one.
public with sharing class UpdateAccountPhoneController {
@InvocableMethod(
label=’Update Account Phone’)
public static List<Account> updatePhone(List<Input> inputWrapper){
Input inp = inputWrapper[0];
Account acc = [Select id, name, phone from Account where name =: inp.accountName limit 1];
acc.phone = inp.phone;
update acc;
return new List<Account>{acc};
}
public class Input{
@InvocableVariable(required=true)
public String accountName;
@InvocableVariable(required=true)
public String phone;
}
}Make sure to add the AccountAgentController class to the Custom Einstein Agent User profile to grant access to this class.
Adding these apex methods into agent Action:
Action1 (Account phone): In the setup, go to the agent and click on the New Action button. A popup will appear to create the action. You can create the action using Flow, Apex, or a Prompt Template. Here, I’m selecting Apex, and you’ll see different reference actions listed. The Apex methods annotated with @InvocableMethod will appear. Select the appropriate one (Account phone) and click Next.
After clicking Next, you will need to add the Agent Action Configuration. The agent action instructions can be provided here, or they can come from the description in the Apex method. You will also need to provide input and output instructions for the action and click on Finish.
Action 2 (Update Account Phone): Click on the New Action button again to create the account phone action. Provide all the instructions as shown above.
Since we haven’t added the description in the Apex method, add the Agent Action Instructions here. Be sure to fill in the required fields, such as Input Instructions and Output Instructions, to provide clear guidance for the action.
You have successfully created two actions for your topic. Now, go back to Step 3.4 (Create Topic), click on the Next button, and you will be given the option to select actions. Select the newly created actions, Account Phone and Update Account Phone, and then click Finish.
After clicking the Finish button, the agent is successfully created. You will see two topics: Account Management and General FAQ, which are standard topics.
Step 6: Test the Agent:
Before activating the agent, you can test it. On the right side of the Agent Builder, you will see the Agent Conversation section. Enter a prompt to check whether the agent is working as expected. This allows you to verify the functionality and ensure the actions and instructions are correctly configured.
The agent is working fine and performing as expected, with records being updated correctly. We can activate the Agent. Create multiple topics and actions to train the agent, and it will provide the correct results. While this article might feel lengthy, I’ve covered everything needed to create an agent with custom actions using Apex. Happy coding! 🙂