Create Custom Actions Using Apex For Agentforce in Salesforce : Bijay Kumar

Create Custom Actions Using Apex For Agentforce in Salesforce
by: Bijay Kumar
blow post content copied from  SalesForce FAQs
click here to view original post



**Summary:** In Salesforce Agentforce, after creating a new agent, users can ask questions, and the agent will process these queries and respond accordingly. This tutorial focuses on creating custom actions using Apex, which allows agents to perform specific tasks such as booking appointments or retrieving account details. **Key Points:** 1. **Custom Actions**: These are specific tasks created to meet business needs. There are two types: - **Standard Actions**: Built-in actions provided by Salesforce. - **Custom Actions**: Tailored actions created by users based on organizational needs. 2. **Methods to Create Custom Actions**: - **Apex**: A programming language used in Salesforce. - **Flows**: A no-code automation tool within Salesforce. - **Prompt Templates**: Reusable prompts written in natural language. - **External Services**: Connecting to APIs without coding. 3. **Creating a Custom Action**: - An Apex class called `CustomAction` is created to retrieve account priorities based on account numbers. This class includes a method annotated as `@InvocableMethod` for integration as a custom action. - A new custom action is set up in Salesforce by referencing the Apex class and filling in necessary details. 4. **Creating a Topic**: - Topics define the tasks agents can perform. Instructions are provided to guide the agent on how to respond to specific queries. - After creating a topic, the corresponding custom action is assigned to it. 5. **Usage**: Once the topic and action are set up, agents can respond to queries, such as checking account priorities, efficiently. **Conclusion**: This tutorial provides a comprehensive guide on how to create custom actions using Apex in Salesforce Agentforce, helping users streamline their operations. **Additional Context**: Salesforce Agentforce combines AI and automation to enhance customer service and operational efficiency. The ability to create custom actions allows organizations to tailor responses to their specific needs, making the agent more effective. **Hashtags for SEO**: #Salesforce #Agentforce #CustomActions #Apex #Automation #CRM #SalesforceTutorial #AIinSalesforce #NoCode #CustomerService #SalesforceDevelopment #TechTutorials #SalesforceCommunity


After creating a new agent in Salesforce Agentforce, when a user asks a query or question to an agent, the agent understands and processes information, takes action to perform tasks, and responds to the user. These actions help agents process the query and give correct responses.

There are different ways to create custom actions in Agentforce. In this tutorial, we will learn about how to create custom actions using Apex for Agentforce in Salesforce.

Custom Actions in Salesforce Agentforce

Actions do the work behind the scenes of an agent. They perform tasks like creating a draft order, booking an appointment, fetching record details, etc.

We will create standard and custom actions in the system according to the organization’s needs, each solving a particular task. Custom actions can be created using Flows, Apex, and prompt templates.

  • Standard Actions: When we enable the agent, the actions already provided by Salesforce are standard actions.
  • Custom Action: Salesforce users create custom actions according to business needs to solve tasks specific to the organization.

The following are ways to create custom actions in Salesforce:

  1. Apex: Salesforce programming language.
  2. Flows: No code Salesforce automation tool.
  3. Prompt Template: Reusable prompts written with natural language.
  4. External Services: There is no code connection to the external API.

Create Custom Action Using Apex For Agentforce in Salesforce

Here, we will use the Apex class to create a custom action.

1. Create Apex Class:

Now, let’s create an apex class that will take the account number and return the account priority to the agents.

The Apex class CustomAction is created to return the priority of an account when a user provides an account number. First, we declared the @InvocableMethod. This method makes this class available as a custom action for AI agents.

When we use this apex class as a custom action in agent builder, that custom action will have a required field called Instruction, which is very important. So, the description will be used as instruction.

Then, the static method (getPriority) takes input from a list collection of account numbers (accNumber) and returns a list of strings (the priority of the account).

In the SOQL query, we passed the user’s account number to retrieve the account priority value. Then, it returns the Account_Priority__c value in a new string list.

public class CustomAction {
    
    @InvocableMethod( label = 'Account Priority'
                     description = 'Return the priority of an accountnumber, when user ask for it, 
                                                       for example user can say what is priority of my account xyz' )
                         
       public static List<String> getPriority( List<Integer> accNumber ){
                             
       Account acc = [Select ID, Name, Account_Priority__c From Account Where 
                                               Account_No__c=:accNumber[0] ];

       return new List<String> { acc.Account_Priority__c };
     }
}

2. Create a New Custom Action For the AI Agent

In the above step, we created the class that executes the query in the backend. Now, we need to create an action in Salesforce for an agent. To do that, search for Action Agent in Quick Find and click on it.

There, you will see the standard actions created by Salesforce. To create a new one, click the New Agent Action button.

Create Agent Actions in Salesforce

Fill in the details below:

  • Referenced Action Type: First, you will only see this field. We need to select Apex because we created an action in the Apex class. As you choose the Apex, another field will be populated.
  • Referenced Action Category: In this field, we need to select Invocable Methods.
  • Referenced Action: Select the label you provided in the Apex class. In our apex class, we provided Account Priority as a label.
  • Then, the Agent Action Label and API Name will automatically populate.
Create Custom Actions Using Apex For Agentforce in Salesforce

Now, you will see the Agent Action Instruction field is auto-populated, and this information comes from the description field in the Apex class. This field is very important because it is what the Atlas reasoning engine will use to understand whether this action will solve this particular task.

In the Loading Text, provide the text that will display when the agent searches the details of your query.

In the Input section, we can see the Apex method was expecting the account number. Then, we have to instruct the Atlas reasoning engine on what information should be passed here.

Then, in the instruction’s Output section, we tell the agent that when you invoke this apex class passing the account number, you will get some output, and we tell it what to do with that output.

After that, click the Finish button to create the action.

How to Create a Custom Apex Agent Action in Salesforce

Now you can see the custom action has been successfully created.

Agentforce build Action for Agent

Topic in Salesforce Agentforce

The topic defines the work these AI agents will do, so each topic defines the agent’s job. Here, we defined the job in natural language. Unlike chatbots, where we define a decision tree, these topics contain a set of instructions and actions that will perform a task.

When creating a topic, we define the instructions the agent will follow if it chooses this topic for the current request.

Create Topic and Assign Apex Custom Action to Topic

In the above steps, we have created one new custom action. Now, we need to assign the created action to the topic.

Navigate to the Agents and select any agent for which you want to assign a created custom action. Here, I have selected Coral Cloud Agent.

Assign Apex Custom Action to AI Agent In Salesforce

After that, click on the Open in Builder button to assign the action.

Salesforce Agent Builder for Agentforce

Here, this agent does not have a topic, and we know that the topic defines what an agent is going to do. The topic contains instructions and actions, so we are going to assign a created action to the topic.

To create a new topic, click the New dropdown and select New Topic.

Create New Topic For Agent in Salesforce

Fill in the topic details as per your requirements. Below, I have shown what should be provided to create a topic.

  • Topic Label: Provide the name of this topic, and the name should be specific.
  • Classification Description: This topic helps in finding account priorities when users ask by giving an account number.
  • Scope: Here, we need to specify the limit of our topic. For example, this topic is for account information. If the user searches for contact information, then the Atlas engine will understand that by using this scope, the agent will not get results in this topic.
Create Topic in Salesforce Agentforce

Instructions

The instructions are the guardrails, or things that help the agent perform their job. Here, you need to give instructions to the agent, and you can provide multiple instructions by clicking on the Add Instruction button.

After adding the instructions, click the Next button to proceed.

Add Instructions to Topic in Salesforce

Now, we need to select the actions for this topic. So, we created the Account Priority action and selected it. If you think your topic will require any other action, then you can select that as well.

After that, click the Finish button.

Select Apex Custom Action for Topic in Salesforce

Now, you can see that one topic has been added to the agent builder. This allows us to create multiple topics and assign multiple actions for one topic in Salesforce.

As you keep adding topics and custom actions to this agent, it can perform more tasks. But for now, it can only perform account priority inquiries.

Topics in Salesforce Agentforce

After adding Topic, Action, and Instruction, we are ready to use this agent. Now, in the Conversation Priview, I have asked a query or question about the priority of account no. 1001.

As a response, you can see it returns the ‘Low‘ priority for the 1001 account number. Also, at the left side, you can see the user prompt that we asked. Then, the atlas engine selects the topic and action that needs to be performed, and the input and output result is displayed.

Building Blocks of Agents in Salesforce

Also, in the account object, you can see that the priority for the 1001 account number is ‘Low’.

Actions of Salesforce Agentforce

In this way, we can create custom Apex actions, assign them to topics, and create instructions for AI agents in Salesforce.

Conclusion

I hope you have an idea about how to create custom actions using the Apex for Agentforce in Salesforce. Using the use case, I have explained what a custom action is, how to create an Apex class, how to create a new action, and how to assign custom actions to agents in Salesforce after creating them.

You may like to read:

The post Create Custom Actions Using Apex For Agentforce in Salesforce appeared first on SalesForce FAQs.


April 09, 2025 at 07:19PM
Click here for more details...

=============================
The original post is available in SalesForce FAQs by Bijay Kumar
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce