Question with Dynamic Options: Example Apex Class

To use a question with dynamic options with your enhanced bot, create an Apex class to populate the options from the objects and fields you specify. The Apex class must return an ID list variable as output for use with the bot.

Required Editions

View supported editions for Einstein Bots.
View supported editions for Messaging.

This Apex class searches cases and filters by priority. It returns the three most recent high-priority cases and stores them in an ID list.

public class CaseSearch {
    @InvocableMethod(label = 'CaseSearch')
    public static List<List<Id>> getCases(){
        List<Id> result = new List<Id>();
        
        List<Case> cases = [SELECT Id FROM Case WHERE Priority='High' ORDER BY CreatedDate DESC LIMIT 3];

        for(Case c: cases) {
            result.add(c.Id);
        }
        
        return new List<List<Id>>{result};
    }
    
}

After you create an Apex class, in the bot, add an Apex action dialog step that references your Apex class and store the output in an ID list bot variable. In the associated Question (Dynamic) dialog step, reference the same ID list bot variable in the Record ID Options input parameter.