Appearance
Exercise 4: Extend Agents with Apex Actions
Coral Cloud Resorts front-desk employees need an easy way to check the weather, so that they can recommend experiences based on the weather forecast. In this exercise, you'll create a custom action using an Invocable Apex class that allows your agent to invoke a third-party weather API.
Step 1: Create the Apex class
From App Launcher, open Code Builder.
Click Launch.
Once it is loaded, open the command palette (
CMD + SHIFT + P
on Mac orCTRL + SHIFT + P
on PC).Search for Apex and click Create Apex Class.
Enter CheckWeather as the class name and accept the default directory.
Replace the default Apex class with the following code:
apexpublic with sharing class CheckWeather { @InvocableMethod( label='Check Weather' description='Check weather at Coral Cloud Resorts at a specific date. The date must be in the future, not today or earlier.' ) public static List<WeatherResponse> getWeather( List<WeatherRequest> requests ) { // Retrieve the date for which we want to check the weather Datetime dateToCheck = (Datetime) requests[0].dateToCheck; // Call a weather service to retrieve the weather through an API call WeatherService.Weather weather = WeatherService.getResortWeather( dateToCheck ); // Create the response for the agent WeatherResponse response = new WeatherResponse(); response.minTemperature = weather.minTemperatureC; response.maxTemperature = weather.maxTemperatureC; response.temperatureDescription = 'Temperatures will be between ' + weather.minTemperatureC + '°C (' + weather.minTemperatureF + '°F) and ' + weather.maxTemperatureC + '°C (' + weather.maxTemperatureF + '°F) at Coral Cloud Resorts.'; return new List<WeatherResponse>{ response }; } public class WeatherRequest { @InvocableVariable( required=true description='Date for which we want to check the temperature. The variable needs to be an Apex Date type with format yyyy-MM-dd.' ) public Date dateToCheck; } public class WeatherResponse { @InvocableVariable( description='Minimum temperature in Celsius at Coral Cloud Resorts location for the provided date' ) public Decimal minTemperature; @InvocableVariable( description='Maximum temperature in Celsius at Coral Cloud Resorts location for the provided date' ) public Decimal maxTemperature; @InvocableVariable( description='Description of temperatures at Coral Cloud Resorts location for the provided date' ) public String temperatureDescription; } }
The
getWeather()
method is defined as an@InvocableMethod
so it can be invoked by your agent. The method returns aWeatherResponse
object. The attributes of theWeatherResponse
class are defined as@InvocableVariable
. The descriptions in both@InvocableMethod
and@InvocableVariable
are important because they allow your agent to understand how to use the action.Save the file (
CMD + S
on Mac orCTRL + S
on PC).Right click anywhere in the code and click SFDX: Deploy This Source to Org.
Step 2: Create the agent custom action
From Setup, open Agents Actions.
Click New Agent Action.
Configure the action as follows:
Field Value Reference Action Type Apex Reference Action Check Weather Agent Action Label Keep default Agent Action API Name Keep default Click Next.
Notice that the instructions fields are pre-filled based on the descriptions provided in the Apex code.
Check Collect data from user for the input (
dateToCheck
).Check Show in conversation for the
temperatureDescription
output.Click Finish.
Step 3: Add the action to your agent
From Setup, open Agents (under Agent Studio).
Click Einstein Copilot in the agent list.
Click Open in Builder.
In the Topics sidebar, click the CustomerServiceAssistant topic.
Click the This Topic's Actions tab.
Click the + button to add an action.
Check the Check Weather action and click Finish.
Step 4: Try it out
In the Conversation Preview panel, enter the following prompt:
txtCheck the weather for tomorrow
Examine the planner and note that the reasoning engine selected the Check Weather action to fullfill the request.
Click the back arrow button to go back to setup.
That's how easy it is to extend agents with custom actions powered by Apex.