Example: Send a Rich Choice Response to a Customer With an Apex Class
In this example, you send multiple links with images associated with food options using an Apex class and rich choice response.
Required Editions
| Available in: Lightning Experience |
| Available in: Enterprise, Performance, Unlimited, and Developer Editions. Required add-on licenses vary by agent type. |
| User Permissions Needed | |
|---|---|
| To build and manage Service Agents: | Manage Agentforce Service Agents AND Manage AI Agents OR Customize Application |
| To define, edit, delete, set security, and set version settings for Apex classes: | Author Apex |
Before you begin:
- Create an Agentforce Service agent. If you want to use an active agent, create a version of it or deactivate it.
- Connect a service agent to an enhanced Messaging channel.
- Turn on adaptive response formats.
- If you haven’t worked with Apex classes before, see Apex Code Overview.
Create an Apex Class
To store and return a list of food options and details, create an Apex class.
public class GetFoodDetailsInvocable {
@InvocableMethod(label='Get Food Details' description='Returns linkURL, linkTitle, image linkURL, image MIME Type, and description text for a given food name')
public static List<FoodDetailResponse> getFoodDetails(List<FoodDetailRequest> requests) {
List<FoodDetailResponse> responses = new List<FoodDetailResponse>();
for (FoodDetailRequest request : requests) {
FoodDetailResponse response = new FoodDetailResponse();
FoodDetail foodDetail = new FoodDetail();
String foodName = request.foodName.toLowerCase();
// Populate food details dynamically from GetFoodOptionsInvocable selections
if (foodName.contains('pizza')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/pizza';
foodDetail.linkTitle = 'Delicious Pizza';
foodDetail.linkImageURL = 'https://www.publicdomainpictures.net/pictures/240000/velka/pizza-1508086895mrm.jpg';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'Our pizza is made with the most flavorful tomato sauce and fresh cheese so that you can indulge all of your senses.'
} else if (foodName.contains('pasta')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/pasta';
foodDetail.linkTitle = 'Tasty Pasta';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1606761568499-5ddd45e43b32';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'Our pasta is topped with the most flavorful tomato sauce and fresh cheese so that you can indulge all of your senses.'
} else if (foodName.contains('tiramisu')) {
foodDetail.linkURL = 'https://tastesbetterfromscratch.com/easy-tiramisu/';
foodDetail.linkTitle = 'Classic Tiramisu';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1571877227200-a0d98ea607e9';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'Our tiramisu is made with the freshest ingredients so that you can indulge all of your senses.'
} else if (foodName.contains('tacos')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/tacos';
foodDetail.linkTitle = 'Authentic Mexican Tacos';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1551504734-5ee1c4a127da';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'Our tacos are made with the freshest ingredients so that you can indulge all of your senses.'
} else if (foodName.contains('quesadilla')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/quesadilla';
foodDetail.linkTitle = 'Cheesy Quesadilla';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1613952273499-5de42c1789c7';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'All of our food is delicious.'
} else if (foodName.contains('nachos')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/nachos';
foodDetail.linkTitle = 'Loaded Nachos';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1606755962773-b35f5d518c46';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'All of our food is delicious.'
} else if (foodName.contains('croissant')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/croissant';
foodDetail.linkTitle = 'Fresh Croissant';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1587049352907-23d8ff0ceac3';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'All of our food is delicious.'
} else if (foodName.contains('ratatouille')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/ratatouille';
foodDetail.linkTitle = 'Traditional Ratatouille';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1633527076476-f847fc6d16a2';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'All of our food is delicious.'
} else if (foodName.contains('creme brulee')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/creme-brulee';
foodDetail.linkTitle = 'French Crème Brûlée';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1609767400465-45e06f28a680';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'All of our food is delicious.'
} else if (foodName.contains('sushi')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/sushi';
foodDetail.linkTitle = 'Japanese Sushi';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1589308078050-cf38fe054d63';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'All of our food is delicious.'
} else if (foodName.contains('ramen')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/ramen';
foodDetail.linkTitle = 'Delicious Ramen';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1602334872613-3ab02f3c95a7';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'All of our food is delicious.'
} else if (foodName.contains('tempura')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/tempura';
foodDetail.linkTitle = 'Crispy Tempura';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1571771439241-89b31f5092c7';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'All of our food is delicious.'
} else if (foodName.contains('dumplings')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/dumplings';
foodDetail.linkTitle = 'Chinese Dumplings';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1617196036851-20ce58a15b1b';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'All of our food is delicious.'
} else if (foodName.contains('peking duck')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/peking-duck';
foodDetail.linkTitle = 'Peking Duck';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1643989946129-73e3a872ad80';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'All of our food is delicious.'
} else if (foodName.contains('spring rolls')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/spring-rolls';
foodDetail.linkTitle = 'Fresh Spring Rolls';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1604328698692-80d9b81baf4d';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'All of our food is delicious.'
} else if (foodName.contains('burger')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/burger';
foodDetail.linkTitle = 'Classic Burger';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1551782450-a2132b4ba21d';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'All of our food is delicious.'
} else if (foodName.contains('hot dog')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/hot-dog';
foodDetail.linkTitle = 'Grilled Hot Dog';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1621248313959-d6c0e480c54e';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'All of our food is delicious.'
} else if (foodName.contains('apple pie')) {
foodDetail.linkURL = 'https://www.foodwebsite.com/apple-pie';
foodDetail.linkTitle = 'Homemade Apple Pie';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1606760225157-145fe46c0b7f';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'All of our food is delicious.'
} else {
foodDetail.linkURL = 'https://www.foodwebsite.com/food';
foodDetail.linkTitle = 'Unknown Food';
foodDetail.linkImageURL = 'https://images.unsplash.com/photo-1606851093480-b24197b5fd6a';
foodDetail.linkImageMimeType = 'image/jpeg';
foodDetail linkDescriptionText = 'All of our food is delicious.'
}
response.foodDetails.add(foodDetail);
responses.add(response);
return responses;
}
return responses;
}
// Request Wrapper Class
public class FoodDetailRequest {
@InvocableVariable(label='Food Name' description='The name of the food item' required=true)
public String foodName;
}
public class FoodDetail {
public String linkURL;
public String linkTitle;
public String linkImageURL;
public String linkImageMimeType;
public String linkDescriptionText;
}
// Response Wrapper Class
public class FoodDetailResponse {
@InvocableVariable(label='Food Details' required=true)
public List<FoodDetail> foodDetails;
public FoodDetailResponse() {
this.foodDetails = new List<FoodDetail>();
}
}
}
In this Apex class, the GetFoodDetailsInvocable class gets a list of food names and returns a list of food items based on the given names. The class takes the FoodDetailRequest wrapper class as input, which contains a food name and variables that can hold food item details. For each food name, the GetFoodDetailsInvocable class looks for a match against predefined food items. If there’s a match, the class assigns the item’s website link, title, image URL, image MIME type, and description text to a custom object defined by the FoodDetailResponse wrapper class.
Make sure that the agent user has access to the Apex class.
Create a Custom Agent Action
Create a custom agent action that uses the Get Food Details Apex class as a reference action.
- Reference Action Type: Apex
- Reference Action: Get Food Details
- Agent Action Instructions: Finds and retrieves details for food options. For each food option, retrieves its name, image, MIME type, URL, and description text. This action is for when a user asks what is on the menu.
- Loading Details: Getting food options
- foodName input instructions: The name of the food option. Select Require input.
- foodDetails output instructions: Details for a food option, including the link, title, image URL, image MIME type, and description text. Select Show in conversation.
Create a Custom Subagent
Create a custom subagent that includes the Get Food Details action.
- Open your Agentforce Service agent in Agentforce Builder.
-
Create a custom
subagent with these properties.
- Classification Description: This subagent provides the user a list of food options and details. For example, use this subagent when the user says: "what's on the menu."
- Scope: Your job is only to provide food options and details.
-
Instructions:
Use Get_Food_Details action to get food options and details.When the user says: "what's on the menu," always give a list of food options. For example, Classic Tiramisu, Homemade Apple Pie, and Delicious Ramen. Then the user is expected to select a food option. Using the food option name selected by user, pass the name to the Get_Food_Details action to get details about the item.When user selects a food option, use the Get_Food_Details action to give the user the link and image for the food option. For example, if user selects Delicious Ramen, then send Delicious Ramen as input to the Get_Food_Details action.
- Include the Get Food Details agent action in the subagent.
To test your agent, activate it and then chat with it on a connected channel.

