Work with Options

Use these Apex classes to implement a Select component or a data provider specified by the datasource key on an External Select component.

tip: The sample app has several examples that use the Select and External Select components.

Option Class

Creates an option for the Select or External Select component.

For the External Select component, construct your data using List<Slack.Option> and return it using Slack.OptionDataResponse.

Alternatively, specify the values or use an expression in the view definition for the Select or External Select component.

Usage

Construct List<Slack.Option> to build a list of values for the Select component.

public class DataProviderOpportunityLookup {
    private static final String NO_OPPORTUNITY = 'Account has no Opportunity';
    private static final String MISSING_OR_INCORRECT_ACCOUNT_ID = 'Account Id is missing or is incorrect';

    /* Use List<Slack.Option> to retrieve all opportunities by accountId.
    Return result in the format List<String, String> to the view. */
    public static List<Slack.Option> getOpportunitiesByAccount(String accountId) {
        List<Slack.Option> oppOptions;

        if (String.isNotBlank(accountId)) {
            List<Opportunity> opps = queryOpportunities(accountId);
            oppOptions = new List<Slack.Option>();

            if (!opps.isEmpty()) {
                for (Opportunity opp : opps) {
                    Slack.Option oppOption = new Slack.Option(opp.Name, opp.Id);
                    oppOptions.add(oppOption);
                }
            } else {
                oppOptions.add(new Slack.Option(NO_OPPORTUNITY, NO_OPPORTUNITY));
            }
        } else {
            oppOptions = new List<Slack.Option>{ new Slack.Option(MISSING_OR_INCORRECT_ACCOUNT_ID, MISSING_OR_INCORRECT_ACCOUNT_ID) };
            System.debug(MISSING_OR_INCORRECT_ACCOUNT_ID);
        }

        return oppOptions;
    }
}

See the sample app for a full example.

Alternatively, use the Slack.Option class to build your option list for the External Select component and return it using Slack.OptionDataResponse.

public class DataProviderAccountLookup {
    public static Slack.OptionDataResponse getAccountsByName(String value) {
        List<Account> accounts;
          if (String.isNotBlank(value)) {
              String name = '%' + value + '%';
              accounts = [SELECT Id, Name FROM Account WHERE Name LIKE :name WITH SECURITY_ENFORCED LIMIT 10];
          }

        List<Slack.Option> accountOptions = new List<Slack.Option>();
        if (accounts != null) {
            for (Account account : accounts) {
                Slack.Option option = new Slack.Option(account.Name, account.Id);
                accountOptions.add(option);
            }
        }
        return new Slack.OptionDataResponse(accountOptions);
    }
}

Option Constructor

Option has the following constructor.

Option(text, value)

Creates an instance of the Slack.Option class with the text and value.

Signature

public Option(String text, String value)

Parameters

text

Type: String

The text to display on the component.

value

Type: String

The value of the option.

Option Methods

The following are methods for Option.

getText()

Gets the display text for the component.

Signature

public String getText()

Return Type

Type: String

getValue()

Gets the value of the option.

Signature

public String getValue()

Return Type

Type: String


OptionDataResponse Class

The data to return for the External Select component using a List<Slack.Option> or List<Slack.OptionGroup>.

Usage

Construct Slack.OptionDataResponse with List<Slack.OptionGroup> or List<Slack.Option>.

important: Constructing a Slack.OptionDataResponse with a combination of Slack.Option and Slack.OptionGroup isn’t supported. If both are present in the Slack.OptionDataResponse, the UI doesn't update.

OptionDataResponse Constructors

The following are constructors for OptionDataResponse.

OptionDataResponse(options)

Signature

public OptionDataResponse(List<Slack.Option> options)

Parameters

options

Type: List<Slack.Option>

OptionDataResponse(optionGroups)

Signature

public OptionDataResponse(List<Slack.OptionGroup> optionGroups)

Parameters

optionGroups

Type: List<Slack.OptionGroup>

OptionDataResponse Methods

The following are methods for OptionDataResponse.

getOptionGroups()

Signature

public List<Slack.OptionGroup> getOptionGroups()

Return Value

Type: List<Slack.OptionGroup>

getOptions()

Signature

public List<Slack.Option> getOptions()

Return Value

Type: List<Slack.Option>


OptionGroup Class

Creates an option group for the Select or External Select component.

For the External Select component, construct your data using List<Slack.OptionGroup> and return it using Slack.OptionDataResponse.

Alternatively, specify the values or use an expression in the view definition for the Select or External Select component.

Usage

Use the Slack.OptionGroup class to build your option groups for the External Select component. See Apex Data Providers for External Select Component.

Alternatively, construct your data using List<Slack.OptionGroup> for the Select component.

public class DataProviderOpportunityLookup {
    private static final String NO_OPPORTUNITY = 'Account has no Opportunity';
    private static final String MISSING_OR_INCORRECT_ACCOUNT_ID = 'Account Id is missing or is incorrect';

    public static List<Slack.OptionGroup> getOpportunitiesByStage(String accountId) {
      List<Slack.OptionGroup> opportunityOptionGroup;

      if (String.isNotBlank(accountId)) {
          List<Opportunity> opportunities = queryOpportunities(accountId);
          opportunityOptionGroup = new List<Slack.OptionGroup>();

          if (!opportunities.isEmpty()) {
              List<Slack.Option> newOptions = new List<Slack.Option>();
              List<Slack.Option> inProgressOptions = new List<Slack.Option>();
              List<Slack.Option> closedOptions = new List<Slack.Option>();

              for (Opportunity opp : opportunities) {
                  switch on opp.StageName {
                      when 'New' {
                          Slack.Option newOption = new Slack.Option(opp.Id, opp.Name);
                          newOptions.add(newOption);
                      }
                      when 'In Progress' {
                          Slack.Option inProgressOption = new Slack.Option(opp.Id, opp.Name);
                          inProgressOptions.add(inProgressOption);
                      }
                      when 'Closed' {
                          Slack.Option closedOption = new Slack.Option(opp.Id, opp.Name);
                          closedOptions.add(closedOption);
                      }
                  }
              }

              Slack.OptionGroup newOptionGroup = new Slack.OptionGroup('New Opportunities', newOptions);
              opportunityOptionGroup.add(newOptionGroup);
              Slack.OptionGroup inProgressOptionGroup = new Slack.OptionGroup('In Progress Opportunities', inProgressOptions);
              opportunityOptionGroup.add(inProgressOptionGroup);
              Slack.OptionGroup closedOptionGroup = new Slack.OptionGroup('Closed Opportunities', closedOptions);
              opportunityOptionGroup.add(closedOptionGroup);
          } else {
              opportunityOptionGroup.add(getEmptyOptionGroup());
          }
      } else {
          opportunityOptionGroup.add(getEmptyOptionGroup());
          System.debug(MISSING_OR_INCORRECT_ACCOUNT_ID);
      }

      return opportunityOptionGroup;
  }
}

See the sample app for a full example.

OptionGroup Constructor

OptionGroup has the following constructor.

OptionGroup(label, options)

Creates an instance of the Slack.OptionGroup class with the label and options.

Signature

public OptionGroup(String label, List<Slack.Option> options)

Parameters

label

Type: String

The text label for the option group.

options

Type: List<Slack.Option>

A list of options to choose from.

OptionGroup Methods

The following are methods for OptionGroup.

getLabel()

Gets the text label of the option group.

Signature

public String getLabel()

Return Type

Type: String

getOptions()

Gets the list of options.

Signature

public List<Slack.Option> getOptions()

Return Type

Type: List<Slack.Option>

See Also

Apex Data Providers for External Select Component.