Calling Agents from Apex or Flow: Expanding Solution Design Possibilities

With the Spring ’25 Release, Salesforce now enables Apex and Flow to call Agentforce Service Agents (ASA) or Agentforce Default (formerly Einstein Copilot)—bringing AI-driven automation beyond the chat window and into core business logic.

While additional costs apply depending on the agent type (as per Salesforce documentation), let’s set that aside for now. What truly excites me is how this unlocks new flexibility in designing solutions within Salesforce!

Previously, agents could invoke Salesforce Apex or Flow components, but now, the other way around is also possible:

🔹Apex - Developers can leverage agents' topics, actions, and reasoning capabilities beyond the chat box—directly within Apex code using custom agent invocable actions. It’s like having the freedom to process agent’s responses in any way through coding. 🔹Flow - Calling agents in Flow is another significant step in extending their functionality beyond the chat window (yet to try this!).

🔗 Read the full article on this feature here: [Salesforce Help Article]


📌 Get the Free Salesforce Developer Edition with Agentforce and Data Cloud

You can now sign up for a free Salesforce Developer Edition org with Agentforce and Data Cloud capabilities to experiment with these features and more.

🔗 Blog on the new Developer Edition with Agentforce & Data Cloud: [Salesforce Blog]

🔗 Sign up for your own Developer Edition here: [Sign Up Here]


📌 Key Namespaces, Classes, and Methods to Access Agents in Apex

Before diving into code, here are some essential namespaces, classes, and methods that Salesforce provides to power this feature:

🔹 Invocable Namespace introduces several action classes for calling invocable agent actions from Apex:

  • Invocable.Action Class – Contains methods to create, update, and retrieve invocable actions.

  • Invocable.Action.Error Class – Provides methods to retrieve errors returned by invocable actions.

  • Invocable.Action.Result Class – Provides methods to retrieve results from invocable actions.

🔗 Full Apex Reference Guide: [View Here]


📌 Sample Apex Code Snippet: Step-by-Step Guide to Calling an Agent Using a Custom Agent Invocable Action:

Step 1: Create an Invocable Action

Define a custom action that calls the Agentforce AI agent from Apex:

Step 2: Set Parameters

Pass user inputs like user message/prompts:

Step 3: Invoke the Agent and Process the Response

🔗 Refer to the Salesforce documentation for complete details: Salesforce Help Article


📌 Maintaining a Session ID While Calling an Agent from Apex

By default, if you do not set the session ID parameter, a new session ID is created for every interaction. This means the agent does not retain context from previous interactions. To enable a smoother conversation—where the user interacts with the same agent across multiple prompt injections via Apex—you need to maintain the session ID across multiple invocations.

Steps to Maintain Context: 🔹 Retrieve the session ID from the first invocation. 🔹 Set the session ID in subsequent calls to the agent from Apex using.

Example: Context Retention in Conversations

In an ideal scenario, conversations require context retention:

🗣️ User Prompt 1: Can you provide a list of cases related to a contact?

🤖 Agent: Sure! Can you provide the Contact ID?

🗣️User Prompt 2: ‘003XXXXXXXXXXXX’.

For the agent to correctly respond to the second prompt, it must remember the first one. Otherwise, it wouldn’t understand why a Contact ID was provided and could generate an incorrect response.

Note: That said, during my testing of calling the agent from Apex, each new prompt I provided was self-contained and did not rely on the agent’s previous response. For instance, in the above sample conversation, if a user had initially provided the following in Prompt 1:

"Can you provide a list of cases related to Contact ID 003XXXXXXXXXXXX?"

This would have been a complete input, meaning the agent would not need to rely on previous context to generate a response (But it’s not an ideal scenario).

👉 Tested Session ID Behavior: Check the video demo below to see how the Session ID affects agent behavior.

Session Id Impact - Demo

📌 Built and Tested a Quick Use Case: Calling Agent from Apex

I developed and tested code to call Agentforce Default (formerly Einstein Copilot) from Apex in a new Developer Edition, performing tasks such as:

Fetching cases based on the owner.

✅ Identifying new or escalated cases related to a contact.

✅ Providing a welcome message to Service Rep.

✅ Displaying a heads-up count of cases assigned to a Service Rep.

And, displayed the data retrieved from the agent—via different prompt injections—on the UI for Service Representative using Lightning Web Component(LWC), providing quick insights into cases.

Future Enhancements: I plan to extend this feature to support additional Agentforce use cases when calling agents from Apex, such as:🔹 Summarizing case data for Service Rep🔹 Case modifications🔹 Generating email drafts, etc.🔹 Interacting with customers on WhatsApp via Agent

👉 Tested the agent response:  Watch the video demo to see the agent in action!

Invoking Agent from Apex & Processing Response - Demo

📌 Agentforce Metadata Deployment

To deploy Agentforce AI across your various Salesforce orgs, you need to deploy key GenAI metadata components such as GenAiPlanner, GenAiPlugin, GenAiFunction, and GenAiPromptTemplate. These components are required to fully integrate Agent AI features into your Salesforce environment by configuring the agent’s reasoning engine, actions, and prompt templates to optimize AI interactions.

Important Notes on API Versions: GenAI components in Salesforce are available only from API version 60.0 and later. If using package.xml for metadata retrieval, set the API version to 60.0 or higher. Similarly, if using Version Control System branches (e.g., Azure Repos (Git), etc.) as the source or target for deployment, ensure that the sourceApiVersion in the sfdx-project.json file is set to at least 60.0 or higher.

[For the full metadata details, refer to the Salesforce Metadata API Developer Guide or check the shared GitHub repository README file for a quick overview.]


📌 Full Code & Implementation

🔗 Explore the full implementation and code here: GitHub Repository


💡 Final Thoughts

The more I experiment with AI in Salesforce, the more I see its potential to reshape solution design. But it’s also very easy to get carried away with these offerings. I’m still experimenting with different scenarios and would love to explore more to identify a real use case that compels businesses to invest in Salesforce AI features to accelerate their growth. I feel some businesses will see game-changing results, while others may still be weighing the investment.

💬 Salesforce folks, have you tried calling AI agents from Apex? What use cases have you explored? Let’s discuss in the comments or DMs—I’d love to try it out!

Salesforce

#Salesforce #Agentforce #EinsteinCopilot #SalesforceAI #ApexDevelopment #SalesforceDev #Flow #AIinCRM #Automation #SalesforceApex #AgentInvocation #ServiceAgent #Trailhead #TDX25 #Nagarro

Suman Saha

Sr. Salesforce Architect at Tavant

4mo

The costing part is still not clear... Salesforce is giving too many features on Agentforce  but somehow I feel documentation related to costing is not upto the mark. If anyone has any ideaon costing part, please reply..

Like
Reply
Kamaal Ahmad Usmani ✨

Salesforce Certified Professional | Agentforce Legend I

4mo

Very informative

To view or add a comment, sign in

Others also viewed

Explore topics