Create Custom Actions Using Apex For Agentforce in Salesforce

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 };