---
schema_version: "1"
id: 59503ba5633c749b6940654d9f6d6f3cfaba23b6b68c786f6309e9424ca5ac08
title: "Example: Send a Rich Choice Response to a Customer With an Apex Class"
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/ai/264-0-0/agent_response_recipe_rich_choice_apex.md
canonical_url: https://help.salesforce.com/s/articleView?id=ai.agent_response_recipe_rich_choice_apex.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 8078f286b1da1a6954df79844a246407c23783c134e083cba16692595208adcf
description: In this example, you send multiple links with images associated with food options using an Apex class and rich choice response.
products:
  - Agentforce
  - Cross Cloud
release: "264"
---
# 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

<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>

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small permission">
        <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="d45421e80">User Permissions
              Needed</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d45421e80 ">To build and manage Service Agents:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d45421e80 ">
              <p>Manage Agentforce Service Agents AND Manage AI Agents</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="d45421e80 ">To define, edit, delete, set security, and set version settings
              for Apex classes:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d45421e80 ">Author Apex</td>

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

Before you begin:

- [Create an Agentforce Service agent](https://help.salesforce.com/s/articleView?id=ai.service_agent_setup.htm&release=264.0.0&type=5 "Agentforce Service agents intelligently support your customers by connecting to messaging and other channels and escalating to reps when necessary. Use the default Agentforce Service Agent template to create an agent designed to resolve common support cases and requests. Or select from more specialized templates to fit your use case."). 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](https://help.salesforce.com/s/articleView?id=ai.agent_parent_deploy_messaging.htm&release=264.0.0&type=5 "Route conversations to your Agentforce Service agents and Agentforce Employee agents with messaging channels, including Enhanced Chat and other enhanced messaging channels. Learn how to use context variables, multiple languages, and progess indicators in messaging channels.").
- [Turn on adaptive response formats](https://help.salesforce.com/s/articleView?id=ai.agent_response_enable.htm&language=en_US).
- If you haven’t worked with Apex classes before, see [Apex Code Overview](https://help.salesforce.com/s/articleView?id=platform.code_about.htm&type=5&language=en_US).

## Create an Apex Class

To store and return a list of food options and details, create an Apex class.

[Create an Apex class](https://help.salesforce.com/s/articleView?id=platform.code_define_package.htm&language=en_US). In the class editor, add this code snippet.

```
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.

1. Reference Action Type: Apex
2. Reference Action: Get Food Details
3. 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.
4. Loading Details: Getting food options
5. foodName input instructions: The name of the food option. Select **Require input**.
6. 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.

1. Open your Agentforce Service agent in Agentforce Builder.
2. [Create a custom subagent](https://help.salesforce.com/s/articleView?id=ai.copilot_topics_add_custom.htm&release=264.0.0&type=5 "Create a custom subagent to specify the actions and instructions an agent needs to meet your users’ particular business needs.") with these properties.
    1. 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."
    2. Scope: Your job is only to provide food options and details.
    3. 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.
    4. 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.