Skip to main content

Time Estimate

Create an Apex Agent Action

Learning Objectives

After completing this unit, you’ll be able to:

  • Create an Apex Agent Action.
  • Add an Agent Action to a subagent.
  • Test the Agent Action.

Build an Apex Agent Action

Now that the Apex is ready, let’s add it to an action. You’ll see where the parameters in the Apex automatically get ported to the action.

  1. From the App Launcher , search for and select Agentforce Studio.
  2. Click Agent Customization.
  3. Click New Version.
  4. Under Subagents, select Answer Weather Questions.
  5. Under Actions Available For Reasoning, click Select action and select Create a custom action.
  6. For Action Name enter Check Weather.
  7. For Description enter This action will return the local weather information.
  8. For Reference Action Type, select Apex.
  9. For Reference Action Category, select Invocable Method.
  10. For Reference Action, select Check Weather.
  11. Click Create and Open.

You’ll notice that in the for Inputs section, the dateToCheck is prefilled with a description from the Apex invocable variable description parameter. You can update or add to the instructions as necessary.

  1. Ensure that the Require input to execute action is defaulted to checked. This is done from the code where the required parameter is set to true.
    Inputs as described above.

For Outputs, there are three that got pulled from the Apex invocable variables, maxTemperature, minTemperature, and temperatureDescription. The Instructions for each are prefilled from the corresponding description parameters and Data Type.

  1. For the third output, temperatureDescription, check Show in conversation.
    temperatureDescription with Show in conversation checked.
  1. Click Save.
  2. Click Commit Version.
  3. Click Commit Version again.
  4. Click Activate.
  5. Click Activate again.

Excellent! The action is complete. You now know what it takes to add your Apex to actions. And you have a better understanding of how the Apex parameters work to help build out the action. Now you can test it.

Let’s Check the Weather

Agentforce Builder allows you to work with and preview the agent right in the tool.

  1. In Agentforce Builder, click Preview.
  2. You should be in Live Test Mode.
  3. In the Describe your task or ask a question box, enter What’s the weather like this Monday? and press enter. The agent might ask for an exact date and give you the option to select one. Select the date for next Monday and submit.

  4. Conversation pane showing the request to What’s the weather like this Monday and the response from the agent showing the temperatures at Coral Cloud.

Take a look at the steps the agent took and notice it selected the Check Weather action to fulfill the request. Agentforce Builder steps showing the Input, Reasoning, Transition to Subagent to the Answer_Weather_Questions subagent then Reasoning Answer Weather Questions.

Well done! You’ve taken a large step toward using all of the Apex functionality your org already has and delivering it up for AI agents to use. You’ve learned some best practices and how to set parameters to get the most out of your agents.

Resources

Share your Trailhead feedback over on Salesforce Help.

We'd love to hear about your experience with Trailhead - you can now access the new feedback form anytime from the Salesforce Help site.

Learn More Continue to Share Feedback