---
schema_version: "1"
id: 95761002dec2a20ef06d237d1f4cbbdcefb0652ccb47aa6c9d433aa89c023597
title: "Custom Agent Invocable Action: Example Apex Classes"
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/ai/264-0-0/agent_custom_invocable_action_apex_example.md
canonical_url: https://help.salesforce.com/s/articleView?id=ai.agent_custom_invocable_action_apex_example.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 433f61a78439e93203203feebee1460cde44e6f21202563d63f85894522de529
description: 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.
products:
  - Agentforce
  - Cross Cloud
release: "264"
---
# 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

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition"><colgroup><col></colgroup><tbody><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: Lightning Experience</td></tr><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: <strong>Enterprise</strong>, <strong>Performance</strong>, <strong>Unlimited</strong>, and <strong>Developer</strong> Editions. <a href="https://help.salesforce.com/s/articleView?id=ai.agent_setup_explore_types.htm&amp;release=264.0.0&amp;type=5" title="Learn about agent types and default templates for specific clouds and common use cases.">Required add-on licenses vary by agent type.</a></td></tr></tbody></table>

![Important](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-ai-generative_ai-264-0-0-production-enus/f8ef6498-d7b2-44e3-9b34-ffd5a850aae9/images/icon_important.png)

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](https://help.salesforce.com/s/articleView?id=ai.migrate_agentforce_default_to_aea.htm&type=5&language=en_US).

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small ">
      <colgroup>
        <col style="width:50%">
        <col style="width:50%">
      </colgroup>
      <thead class="slds-text-title_caps ">
        <tr class="">
          <th scope="colgroup" class="slds-cell-wrap" colspan="2" style="text-align:center;vertical-align:top;" id="d153419e83">User Permissions
            Needed</th>
        </tr>
      </thead>
      <tbody>
        <tr class="">
          <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d153419e83 ">To create an agent invocable action associated with Agentforce
            (Default):</td>

          <td class="slds-cell-wrap" style="vertical-align:top;" headers="d153419e83 ">
            <p>Manage AI Agents AND the required permissions for your
              agent type</p>
            <p>OR</p>
            <p style="margin-bottom:0;">Customize Application</p>
          </td>

        </tr>
        <tr class="">
          <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d153419e83 ">To create an agent invocable action associated with Agentforce
            Service Agent:</td>

          <td class="slds-cell-wrap" style="vertical-align:top;" headers="d153419e83 ">
            <p>Manage Agentforce Service Agents AND Manage AI Agents</p>
            <p>OR</p>
            <p style="margin-bottom:0;">Customize Application</p>
          </td>

        </tr>
      </tbody>
    </table>

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());       }     } }`