Agentforce provides a suite of out-of-the-box autonomous AI agents, along with a set of tools that admins and developers can use to create and customize agents. One of these tools is Agent Builder, where you can customize agents using the tools that you know, like Flow, prompt templates, Apex, and APIs.
In this blog post, we’ll cover some fun and advanced use cases that you can implement using custom agent actions and code. You’ll learn how to:
- Empower an agent to create custom metadata (built with prompt templates, Apex classes, and APIs)
- Equip an agent to monitor Marketing Cloud journeys (built with Apex and APIs)
- Surface an agent’s response in a custom LWC (built with Apex, APIs, platform events, and LWC)
These use cases were presented at Dreamforce 2024 in a session called “Advanced Custom Agent Actions for Developers,” and they were 100% ideated by one of our developer trailblazers, Luciano Straga.
Use Case 1: Empower an agent to create custom metadata
Our first use case is about creating a set of actions that allow agents to build custom metadata on the Salesforce Platform when requested in plain language. This use case could be interesting for creating an internal agent that empowers builder roles to create metadata with less clicks. To implement this capability, you need to create two custom agent actions:
- Generate Object Metadata (prompt template action): This custom action invokes a flex prompt template that crafts the XML code that needs to be sent to the Metadata SOAP API to create the requested metadata. The prompt template receives as parameter the description of the metadata to be created in plan language (in the form of a free text input), and outputs XML.
This is an example response that the LLM could generate:
Create Metadata (Apex action): This custom action invokes an Apex class that performs the Metadata SOAP API invocation. It receives as parameter the XML code that matches the definition of the metadata that needs to be created, and it returns a success message, or throws an exception in the case of an error.
Then, you will need to make the actions available for a topic like this one:
Once the actions and topic are ready, you can try them out in Agent Builder:
- To start, you’ll have to specify the metadata that you want to create in natural language, for example: “Create a custom object called ‘Community Conference’ that has a ‘Name’ field (text), a ‘Date’ field (date), and a ‘Location’ field (text).”
- The agent will identify that this request can be completed using the created topic and its actions. Concretely, it will decide that it needs to execute the Generate Object Metadata prompt template action followed by the Create Metadata Apex action.
- The agent will execute the prompt template linked to the Generate Object Metadata action, passing in the user input, to get the required XML code. For example:
- The agent will execute the Create Metadata action, passing in the XML code that was crafted in the previous step. The Apex code will invoke the Metadata SOAP API, passing in the received XML, and using the running user session ID. The Apex method will then return a success message or throw an exception in case of failure.
- Finally, the agent will elaborate a response in natural language, explaining the success or failure of the metadata creation, using a large language model (LLM).
Learnings and considerations
- You will need multiple iterations until you get the prompt template and actions to do what you want them to do. Using prompt engineering techniques like few shot prompting is key to improving results. Prompt Builder and Agent Builder were key to facilitating iteration and testing.
- There needs to be a strong dependency between the two custom actions, and that dependency needs to be specified as instructions in the topic. To solve this, we added an instruction that says the Generate Object Metadata action should be followed by the Create Metadata one. Many use cases will follow this same technique to help the Agentforce reasoning engine craft the right plan of actions.
- Because actions execute asynchronously, one after the other, we overcome some limits that could arise if executing all the logic in one transaction. For example, if you try to just have one action that calls the prompt template from Apex and then creates the metadata, you run into a callout error. But having the two actions execute asynchronously fixes this problem.
- Having the two actions separated also gives the agent flexibility. The user could provide the XML themselves as part of their input, or the agent could craft the XML using a different action. The XML, no matter its source, could then be used in the Create Metadata action to perform the API call. Indeed, for this concrete demo, the prompt template instructions were limited to just creating custom objects, but you could have different prompt template actions to create different types of metadata and let the agent choose which action to pick based on the user’s prompt. The agent could even create multiple different types of metadata at once by chaining more of these actions.
- Bear in mind that creating objects in production is a bad practice. This is just a demo example to showcase the power of combining different technologies to build agents, and you should adapt it to your specific use cases.
Use Case 2: Equip an agent to monitor Marketing Cloud journeys
The second use case consists of a custom action that uses the Marketing Cloud REST API to retrieve journeys status and population. This action could be part of an internal agent that Marketing Cloud users get help from. In this case, you just need to create a single Apex action.
- Get Marketing Cloud Journeys (Apex action): Invokes the Marketing Cloud REST API to retrieve journeys information. It needs to get an auth token for the running user.
Once the action is ready, you can assign it to a topic that is designed to help marketers complete their jobs faster. Then, you can try the use case in Agent Builder.
- To start, you’ll have to ask something like this: “Show me a table of Marketing Cloud journeys.”
- The agent will identify that this request can be completed using the created topic and its action. Concretely, it will decide that it needs to execute the Get Marketing Cloud Journeys Apex action.
- The agent will execute the Apex class linked to the Get Marketing Cloud Journeys action, passing no user input, and obtaining the journeys formatted in JSON.
- Finally, if the API call was successful, the agent will elaborate a response that includes the journeys, using an LLM.
Learning and considerations
- The beauty of this use case is that you can manipulate the response on the fly. You can continue the conversation with the agent, saying things such as “Show this info formatted as table,” “Sort the table by name ascendant,” or “Filter by population > 500.” The agent will be smart enough to not repeat the API call, and it will be able to reformat the response according to your needs. Try it!
Use Case 3: Surface an agent’s response in a custom LWC
The third use case uses multiple technologies to show how it’s possible to make an internal agent interact with other LWCs on the page. In this case, you need to create the following action.
- Generate Inspirational Message (Apex action): Invokes the Models API to get an inspirational message, and then publishes an Inspirational_Message__e platform event containing the generated message.
In this use case, you’ll also need a listener for the message. For instance, you can place an LWC on the utility bar that uses the empApi to listen to the message and shows a toast when a message is received, as follows:
Once the action is ready, you can assign it to a topic that is designed for the use case. Then, you can try the use case in Agent Builder.
- To start, you’ll have to ask something like this: “Show me an inspirational message.”
- The agent will identify that this request can be completed using the created topic and its action. Concretely, it will decide that it needs to execute the Generate Inspirational Message Apex action.
- The agent will execute the Apex class linked to the Generate Inspirational Message action, that in turn will generate the message and publish a platform event. The action will return a success message.
- The LWC that’s listening on the utility bar will receive the message and show it in a toast.
- Finally, the agent will elaborate a response that includes a success message using an LLM.
Learning and considerations
- Note that in general it’s not recommended to have
empApi
listeners in components that are available to all users, as it can rapidly consume all concurrent streaming event listeners limits. - Bear in mind that this use case might not provide the best user experience for your customers. However, you might find use cases that work for your specific business needs using the same technologies. At the moment, there’s no way to customize the UI components where agent’s responses are shown, however, this is something on the roadmap.
Internally vs externally exposed agents
All the use cases we covered in this post have been designed and implemented to be used by an internal agent (an employee copilot). Under these assumptions, the identity of the user who queries the agent is always known, and the actions are always executed under the context of that user, honoring their permissions and security settings.
Conclusion
In this blog post, you saw how Agentforce and its advanced custom agent actions can be created by developers to customize agents using the tools you know, such as Flow, prompt templates, Apex, and APIs. These are just some examples, but the options are infinite.
To know more about these concrete use cases, watch this codeLive episode on YouTube, and check the GitHub repository that contains the code for these examples.
The following resources can help you learn more about Agentforce:
- Agentforce Developer Center: Includes blog posts, podcasts, videos, and Trailhead modules
- Dreamforce ’24 – Agentforce and Data Cloud Playlist: Includes 16 Dreamforce videos about Agentforce and Data Cloud
Finally, get some hands-on experience by participating in the #BuiltWithAgentforce Quest, where you’ll build your first agent and add some actions to it. Complete the Quest by December 31, 2024 and enter for a chance to win a TrailblazerDX pass or an Agentblazer Hoodie.
About the author
Alba Rivas works as a Principal Developer Advocate at Salesforce. You can follow her on GitHub or LinkedIn.