Agentforce agents can serve two primary purposes for a business: assisting employees and interacting with customers. Employee-facing agents help employees enhance productivity by automating tasks, summarizing data, and assisting with actions within Salesforce, operating with the user’s permissions. Customer-facing agents, often found on websites or apps, act as “digital labor,” handling tasks like answering questions, providing shopping assistance, or qualifying leads, using their own specific user profiles and permissions.
Previously, interacting with these agents often required manual prompting through interfaces like the Salesforce Agentforce chat modal. Now, invocable agent actions allow you to trigger any active Agentforce agent directly from Apex and flows.
These capabilities unlock powerful use cases, including:
- Triggering agentic flows from Quick Action buttons, and Lightning web components
- Interacting with agents from external systems or other Salesforce orgs via REST
- Orchestrating agent-to-agent interactions.
Invoking Agentforce agents
Salesforce has always been a platform for both low-code and pro-code developers, and Agentforce is no different. You can invoke Agentforce agents with no code using Flow or from within an Apex method. In either scenario, you’ll specify an agent and invoke it with the user’s message and optionally a session ID.
The user’s message is a prompt, but what is the agent session ID and where does it come from? The session ID ties together each interaction between a user and an agent. When a user sends their first message, a unique session ID is generated. You’ll pass this ID along with subsequent messages, allowing the agent to remember previous exchanges and maintain context. Many use cases don’t require multi-turn conversations, but for those that do, you’ll need to use the session ID.
Using AI Agent actions in flows
You can invoke any active Agentforce agent from a flow using the standard AI Agent action. First, create an Action element and search for the AI Agent Action folder. You’ll find an action for each active Agentforce agent. After selecting an agent, specify a user message and an optional session ID input. As a best practice, you should create input variables for the user message and session ID, so that your flow can populate them dynamically. You should also include an agent response output variable to capture the agent’s response.
AI Agent action-enabled flows are a powerful way to embed agentic AI throughout your Salesforce orgs. You can use Flows to trigger updates to records via Quick Action buttons and embed them in screen flows. You can even facilitate limited agent-to-agent communication by using them in a flow-based agent action.
Consider the AI Agent action whenever you need to add quick, agent-orchestrated automation in Salesforce. Note that all the same Flow best practices still apply. You’ll need to be mindful of Flow timeouts and implement error handling.
Using the Invocable action in Apex
You can invoke your Agentforce agents in Apex using the Invocable Action API.
First, create a custom generateAiAgentResponse action. To create the action, you’ll need to specify an agent API name, which you can find on the agent’s detail page in Salesforce Setup.
Next, pass along the required user message and optional session ID to the action as invocation parameters. Finally, invoke the action.
Invoking your agents in Apex provides several capabilities that flows do not have. First, you can expose your agent via REST by deploying a REST annotated Apex method that accepts a user message and session ID, and returns the agent’s response. Voila! You’ve created a service that interacts with your agent beyond Salesforce.
Just remember that agents should always use the permissions of the logged-in user. This means that your integration should implement the OAuth 2.0 Web-Server or User-Agent flow to force the user to authenticate. This is crucial for maintaining proper security and data access control.
Apex also allows you to call your agent from Lightning web components. Want to not only invoke your agent, but send it a custom message? You can do so by building a Lightning web component that calls an Apex method that uses a generateAiAgentResponse invocable action.
Consider the generateAiAgentResponse invocable action whenever you need the flexibility and performance that Apex provides — whether that’s building a REST service or a Lightning web component.
Conclusion
Flows and Apex now empower you to seamlessly integrate Agentforce agents into your workflows. These new integration points open up powerful opportunities for automation and customization, catering to both low-code and pro-code developers. Whether you’re streamlining tasks with Flow or building custom integrations with Apex, these tools make agents your productivity partners. Experiment and see how you can leverage these capabilities to enhance your Salesforce automations.
Resources
- Documentation: Calling an Agent from a Flow or Apex Class
- Documentation: Custom Agent Invocable Action: Example Apex Class
- Documentation: Invocable Namespace
About the author
Charles Watkins is a Lead Developer Advocate at Salesforce. You can follow him on LinkedIn and GitHub.