Custom Agent Invocable Action: Example Apex Classes

Call a custom agent invocable action from an Apex class to complete a task automatically with your Agentforce Service agent or default Agentforce agent. The Apex class must return the agent’s response.

Required Editions

Available in: Lightning Experience
Available in: Enterprise, Performance, Unlimited, and Developer Editions. Required add-on licenses vary by agent type.
Important
Important Starting June 17, 2025, Agentforce (Default) will not include new features or improvements and isn’t available in new Salesforce environments. We recommend migrating to Agentforce Employee agent for continued enhancements and support. If you plan to transition to Agentforce Employee agent or make related license changes, complete the migration of your existing Agentforce (Default) agents in advance to avoid potential disruption in agent availability. If you can't complete the migration in advance, you can create new agents after the transition, which can involve some downtime. See Migrate from Agentforce (Default) to Agentforce Employee Agent.
User Permissions Needed
To create an agent invocable action associated with Agentforce (Default):

Manage AI Agents AND the required permissions for your agent type

OR

Customize Application

To create an agent invocable action associated with Agentforce Service Agent:

Manage Agentforce Service Agents AND Manage AI Agents

OR

Customize Application

Custom agent invocable actions support context variables only. To create a context variable, create a custom field in the Messaging Session object and then add it as a variable in Agentforce Builder. In these examples, caseId and leadId are the API names of custom fields.

This Apex class calls the default Agentforce agent using version 1.1.0 of the custom agent invocable action. It returns an email using the CaseEmail Apex class and a case ID.

public class AgentIAInvoker {
    public static void invokeJavaAction() {
      try {
        // Create an instance of the invocable action with type ‘generateAiAgentResponse’, API name ‘Acme_Agent’, and version 1.1.0
        Invocable.Action action = Invocable.Action.createCustomAction('generateAiAgentResponse', null, 'Acme_Agent', '1.1.0');
        
        action.setInvocationParameter('userMessage', 'Generate an email with a summary of the case');
        action.setInvocationParameter('caseId', '500VW0XXXXXXXXXXXX');
        action.setInvocationParameter('apexClassName', 'CaseEmail');
        
        // Execute the action
        List results = action.invoke();
        
        // Handle the response
        System.debug('Result is: ' + results[0].getOutputParameters().get('structuredAgentResponse'));
      }
    }
}

This Apex class calls an Agentforce Service agent using version 1.0.0 of the custom agent invocable action. It returns a summary based on the lead ID.

public class AgentIAInvoker {
    public static void invokeJavaAction() {
      try {
         
          // Create an instance of the invocable action with type ‘generateAiAgentResponse’ and name ‘Agentforce_Service_Agent_new’
          Invocable.Action action = Invocable.Action.createCustomAction('generateAiAgentResponse', null, 'Agentforce_Service_Agent_new', '1.0.0');
          
          action.setInvocationParameter('userMessage', 'Summarize my contact');
          action.setInvocationParameter('leadId', '500VW0XXXXXXXXXXXX');
          
          // Execute the action
          List<Invocable.Action.Result> results = action.invoke();
          Invocable.Action.Result result = results[0];

          // Handle the response
          if (result.isSuccess()) {
              // Retrieve the Session ID and Agent Response
              System.debug('Output Session ID: ' + result.getOutputParameters().get('sessionId'));
              System.debug('Output Agent Response: ' + result.getOutputParameters().get('agentResponse'));

          } else {
              System.debug('Java action execution failed: ' + result.getErrors());
          }
      } catch (Exception e) {
          System.debug('Error invoking Java action: ' + e.getMessage());
      }
    }
}