Invoking Agentforce Agents from Apex Unlocking AI Automation in Salesforce
KeshavJuly 30, 2025
Salesforce Agentforce empowers users to bring conversational intelligence and task automation directly into everyday workflows. With recent enhancements, it’s now possible to invoke Agentforce agents programmatically from both Apex and Flow.
In this blog we will invoke the Agent from Apex class.
What Are Agentforce Agents?
Agentforce agents in Salesforce are specialized AI entities built to automate and augment business processes:
Employee-Facing Agents: Tasks such as summarizing records, automating common activities, and extracting insights, operating under the permissions of the Salesforce user.
Customer-Facing Agents: These act as digital labor on websites or apps, answering questions, assisting shoppers, or qualifying leads while using dedicated user profiles and permissions.
Why Invoking Agents from Apex or Flow Matters
Previously, Agentforce was mostly accessed through UI chat modals. Now, direct invocation from Flow or Apex enables:
Orchestration of complex automation behind buttons, web components, or external endpoints.
Seamless integration of agentic intelligence into any business logic path in Salesforce.
Persistent and context-aware interactions (using session IDs), allowing agents to maintain conversational context across invocations.
Enable Agentforce (Default) in Your Org
In the Quick Find box Search for Agentforce Agents
Select Agentforce Agents
Turn on the toggle button Enable the Agentforce (Default) Agent
Now you will see the Agent named Agentforce (Default)
How to Invoke Agentforce Agents
Invoking via Apex
Free newsletter
Get Salesforce insights delivered weekly
Join 500+ Salesforce professionals and stay ahead of the curve.
No spam. Unsubscribe any time.
Go to your Developer Console
Create an Apex Class named AgentInvoker
Copy below Apex Code in your apex class
AgentInvoker.apxc
publicclassAgentInvoker{publicstaticStringgetAgentResponse(){try{// Create an instance of the invocable action with type 'generateAiAgentResponse' and name 'Copilot_for_Salesforce'Invocable.Action action =Invocable.Action.createCustomAction('generateAiAgentResponse', 'Copilot_for_Salesforce'); action.setInvocationParameter('userMessage', 'Summarize my case'); action.setInvocationParameter('CaseId', 'your caseId');if(sessionId !=null){// Add session id only if it exists action.setInvocationParameter('sessionId', sessionId);}// Execute the actionList<Invocable.Action.Result> results = action.invoke();Invocable.Action.Result result = results[0];// Handle the responseif(result.isSuccess()){// Retrieve the Session id and Agent ResponseSystem.debug('OutputSessionId: ' + result.getOutputParameters().get('sessionId'));System.debug('OutputAgentResponse: ' + result.getOutputParameters().get('agentResponse'));return 'SuccessfullyInvokedAgent!';}else{System.debug('Java action execution failed: ' + result.getErrors());return 'InvokingAgent failed!';}}catch(Exception e){System.debug('Error invoking Java action: ' + e.getMessage());return 'Error in Invoking the Agent';}}}
Create a trigger named LeadTrigger
Copy below code and paste it to your trigger
LeadTrigger.apxt
trigger LeadTrigger on Lead(before insert){if(Trigger.isBefore &&Trigger.isInsert){AgentInvoker.getAgentResponse();}}
Conclusion
Invoking Agentforce Agents from Apex brings the power of conversational AI directly into your Salesforce backend logic. With just a few lines of code, you can trigger intelligent, context-aware responses that enhance user productivity and streamline business processes.