Agentforce is a conversational AI assistant for Salesforce. It understands your organization’s metadata and data permissions, and you can ask it to perform actions using natural language. Within the guardrails of the Einstein Trust LayerAgentforce comes prebuilt with a library of standard actions to find or summarize records, draft sales emails, aggregate data, and more. However, what sets Agentforce apart is its extensibility for developers through Agent Builder.

With Agent Builder, developers can enhance Copilot’s capabilities by creating custom actions using Apex, flows, or prompt templates. This unique extensibility empowers Salesforce Developers to tailor Agentforce to their business needs, creating bespoke conversational AI assistants.

In this blog post, we’ll guide you through the process of designing and building copilot custom actions with Apex. This will enable you to build conversational AI assistants for your business needs using skills you already have as a Salesforce Developer.

Building a basic “Say Hello” custom action with Apex

The screenshot below shows how a copilot is able to greet the user via a custom Apex action, “Say Hello.” Let’s walk through how I built this simple custom action using Apex.

ALT TXT - Screenshot showing a basic custom action for greeting the user

Below is a very basic Apex class that I implemented for this task.

Code explanation

  • Notice that we have implemented a method annotated with @InvocableMethod (see docs) in our Apex class. This makes the action available in Agentforce Studio for configuration.
  • You can write labels and descriptions for your Apex method using @InvocableMethod and input variables using @InvocableVariable (see docs). Agentforce leverages these descriptions to match user conversation intents with the right actions.

Configuring Apex actions in Agent Builder

Next, you can configure the Apex action that you created in the previous step as a copilot action in Agent Builder. This makes the action available to be added to the copilot’s library of actions. The documentation shows the steps necessary to configure an Apex action for your copilot.

Notice in the screenshot below where you can add instructions for your action, inputs, and outputs. This is a very critical step. Agentforce leverages these descriptions to match user conversation intents with the right actions. You may have to tweak this until you get your actions working with various utterances. The best practices for writing instructions are documented (see docs).

Configuring an Apex action in Copilot Builder

Adding actions to Agentforce for testing

Next, add actions to your copilot using Agent Builder to start testing your action. Once added, the action shows up in This Copilot’s Actions tab. The screenshot below shows how you can add your custom action from the Copilot Action Library tab.

Adding actions for testing in Copilot Builder

Note for best performance, we recommend assigning no more than 15 actions to a copilot. You can read more about considerations and limits in the documentation (see docs).

Testing custom actions in Agent Builder

With Agent Builder, you can test and troubleshoot the planner service’s ability to invoke the custom action. For additional debugging, enable the enhanced event logs. You can view the event logs from Agent Builder.

Chaining multiple custom/standard actions

A key capability of Agentforce is chaining multiple actions together from the request. This is enabled by its reasoning engine, which decides which actions to launch and when. It analyzes the conversation in order to build a plan to accomplish the user’s request. You can find full details about how Agentforce’s reasoning engine works via the documentation.

From the end user’s perspective, chain actions allow you to perform tasks through natural conversation. For instance, you can ask Agentforce to find a record, provide a summary of details, and automatically draft an email with that summary.

The screenshot below shows an utterance telling Agent Builder: “Retrieve the record ‘DYNAMO X4,’ summarize the record, and draft an email with the summary in the body for contact Lauren Bailey.” By chaining together three standard actions — Identify Record by Name (used twice, once to find the product record and other to find the account record), Summarize Record, and Draft or Revise Sales Email — the planner was able to generate the requested response.

Test and troubleshoot chaining of copilot actions in Copilot Builder

Let’s look at an example to explore the process of chaining multiple standard and custom actions. In the screenshot below, I ask Agent Builder to create a reseller order and draft an email.

Example use case for standard and custom actions chaining

I’ve built the above conversational AI experience for account executives using the E-Bikes sample app, allowing them to be able to create a reseller order and draft an email via natural conversation.

To accomplish the end goal, I had to create a custom Apex action for creating reseller orders as shown below. Note that I have intentionally skipped a few parts of the code for simplicity.

Code explanation

  • The createOrder method is annotated with @InvocableMethod to make it available as a Flow action. It creates a reseller order for specific products.
  • The method is enclosed in a try-catch block to handle exceptions. If an exception occurs, the code sets an error message in the errorMessage variable of the Output class, indicating the issue and advising users to contact the admin with the error message.
  • After successful processing, the code queries the created Order records and populates the order variable in the Output class.

Apex best practices when writing for Agentforce

Below are some best practices that you should follow when writing Apex for Agentforce.

  • Write clear, descriptive labels and descriptions for the Invocable Apex classes and variables. To learn more about the best practices around writing detailed instructions for your action and variables, check the related docs.
  • Follow Apex security best practices, such as using sharing mode and running in the user mode.
  • Write bulkified Apex code to prevent hitting governor limits.
  • Keep the experience fast by avoiding complex, time-consuming processes.
  • Make sure you write optimized SOQL. This becomes critical if you are working against objects with large volumes of data (LDV objects).
  • Thoroughly test Apex code in isolation using unit and integration tests.

Conclusion

Agentforce custom actions allows developers to build conversational AI assistant apps without needing multiple tools for agent orchestration. By handling prompt management techniques and LLM APIs internally, Copilot custom actions simplify the process, so developers can focus on building the AI assistant experience for the business use case.

We are getting started in this journey, and if you have any feedback about Agentforce, please reach out via the Trailblazer community.

Additional references

About the author

Mohith Shrivastava is a Developer Advocate at Salesforce with a decade of experience building enterprise-scale products on the Salesforce Platform. He is presently focusing on the Salesforce Generative AI-based applications at Salesforce. Mohith is currently among the lead contributors on Salesforce Stack Exchange, a developer forum where Salesforce Developers can ask questions and share knowledge. You can follow him on X (Formerly Twitter) and LinkedIn.

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS