Invoke Agentforce Agents with Apex and Flow

AgentForce agents can serve two main purposes for a business: assisting employees and communicating with customers. Employee-facing agents help employees increase productivity by automating tasks, summarizing data, and assisting with actions in Salesforce, working with user permissions. Customer-facing agents, often found on websites or apps, act as “digital labor,” handling tasks such as answering questions, providing purchasing assistance, or qualifying leads, using their own specific user profiles and permissions.
Previously, communicating with these agents often required manual prompting through interfaces such as the Salesforce AgentForce chat model. Now, Invokable Agent Actions allow you to trigger any active AgentForce agent directly from Apex and flow.
Table of Contents
These capabilities unlock powerful use cases, including:
- Triggering agentic flows from Quick Action buttons and Lightning Web Components
- Communicating with agents in external systems or other Salesforce organizations via REST
- Orchestrating agent-to-agent interactions.
Invoking AgentForce Agents
Salesface sales is still a platform for low developers and pros code and agentforce is not different. You can invoke AgentForce agents using Flow or without any code from an Apex method. In either case, you will 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 an agent session ID and where does it come from? The session ID associates every interaction between a user and an agent. When a user sends his first message, a single session ID is generated. You will pass this ID with subsequent messages, allowing the agent to remember previous exchanges and maintain context. Many use cases do not require multi-turn conversations, but for those that do, you will need to use session IDs.
Using AI Agent Actions in Flows
You can invoke any active AgentForce agent from a flow using a standard AI agent action. First, create an action element and find the AI Agent Actions folder. You will find an action for each active AgentForce agent. After selecting an agent, specify the user message and optional session ID inputs.
As the best practice, you need to create input variables for the user message and the session ID for your flow can dynamically. You also need to include the output variable of an agent’s response to capture the agent’s response.
AI Agent Action-enabled flows are a powerful way to embed agentic AI into your Salesforce organizations. You can use flows to trigger updates to records through quick action buttons and embed them into screen flows. You can also simplify limited agent-to-agent communication by using them in flow-based agent actions.
Consider AI Agent Actions whenever you need to add fast, agent-orchestrated automation to Salesforce. Note that all the same flow best practices still apply. You will need to be aware of the terms of flow and implement error management.
Using Invocable Actions in Apex
You can call your AgentForce agents in Apex using the Invocable Actions API.
First, create a custom generateAiAgentResponse action. To create an action, you will need to specify the agent API name, which you can find on the agent details page in Salesforce Setup.
Next, pass the required user message and optional session ID to the action as invocation parameters. Finally, invoke the action.
Call your agents in APEX provides some capabilities that the flow has not. First of all, you can expose your agent to the rest of the APEX’s Nat of Appeals that accepts a user’s message and the response of the session. The tour is played! You created a service that interacts with your agent out of seller.
Remember that agents will always use the consignment authorizations. This means that your integration must implement a orautouth internet server or a user’s agent trunk to force the user to try. This is important to hold the good safety and control of data income.
Apex allows you too to call your agent from the Internet Ingredient lightning. You don’t want to call you to call your agent but also send a custom message? You can do this creating a component of Internet lightning that you call a method of apex that uses the action that can be generateaeaecaeagon.
considers instructable actions of Generateaa Generateaa every time You need flexibility and performance provided by APEX, be of creating a resting service or a component of Internet lightning.
Conclusion:
Agentforce agents revolutionize how businesses interact with both employees and customers by automating and streamlining workflows within and beyond Salesforce. With the introduction of invocable agent actions, developers can now trigger agents directly from Apex and Flow, enabling smarter, faster, and more integrated experiences. Whether it’s launching agentic flows from buttons or components, integrating external systems via REST, or coordinating agent-to-agent interactions, these capabilities open up endless possibilities.