Expressions

Expressions enable you to access property values and other data for dynamic output. Use expressions to reference the data from your data providers and view definition schema throughout a view. You can also use expressions to calculate, format, or transform data fields.

You don't need to implement a view data provider to use expressions that reference views.properties since Apex SDK for Slack does that for you.

There are two types of expressions.

  • Data provider expressions
  • Formula expressions

Expressions must be enclosed in brackets like {!expression}.

The {!} wrapper tells the framework that the string is an expression that must be resolved.

To write an expression, use the key of the data provider reference. The framework resolves the expression to the value from the corresponding data provider and renders the value in the view.

Data provider expressions follow this period-delimited string syntax.

  • <key> maps the expression to a data provider.
  • <tokens> are references to properties from the data provider.

For example, {!view.properties.*} represents an expression that resolves data passed into the view. See View Expression Syntax.

To determine the valid properties, check the documentation for the data provider you're using. For example, {!record.data.fields.Name.value} is from the record data provider backed by UI API.

For a data provider that returns a primitive, the expression looks like {!key}.

For a data provider that returns an object, the expression uses properties specific to the object definition, such as {!contact.Name}.

You can also reference an expression of any type within a string, which makes it easy to construct variable strings.

For more information, see Implement a Data Provider.

All expressions in view definitions are case sensitive. For example, {!label.labelName} works but {!Label.labelName} doesn't.

View expressions are special data provider expressions that are resolved from data passed into the view.

View expressions follow this period-delimited string syntax.

  • <category> maps to the type of top-level data passed into the view. Currently, only properties is supported.
  • <tokens> are the references to the top-level view data. For example, {!view.properties.objectApiName}.

Here's an example view that contains several expressions.

Custom label expressions resolve to custom labels in Salesforce.

  • <namespace> is the optional namespace. c is the default namespace. Include the namespace if your org has a namespace or to access a label in a managed package.
  • <labelName> is the name of the label. See Work with Custom Labels.

Formula expressions help you calculate values dynamically, such as to enforce a validation rule or apply a value on a field when a user creates a record. It is based on the Salesforce Formula Engine library [GitHub Repo].

For details of the formula syntax, operators and functions, see Formula Operators and Functions in Salesforce Help.

Formula expressions follow this general format:

Apex SDK for Slack currently has limited support for formula expressions. Expressions that includes string concatenation using the + operator and functions like SUBSTITUTE() and LABEL() are not yet supported.

Here are a few formula expression examples.

  • This example uses a data provider expression to resolve to a name value on a record field. For example, John Smith. In general, any valid data provider expression can be referenced by a formula field.

  • This formula expression calls the FORMAT function.

    label.help.headerTemplate is a label provider expression that resolves to a text template pattern. commandInfo.commandName is an iteration data provider expression, where commandInfo is the iteration item. If label.help.headerTemplate resolves to "*Command*: {0}" and commandInfo.commandName resolves to "search", then this expression evaluates to a String value of "*Command*: search".

We recommend using the FORMAT formula function to handle your string templating use cases.

You can bind your expressions to a view by referencing its schema. Here's a simple view with a default string passed into its view.properties schema.

Let's take a look at the expression in the view.

The expression resolves to Hello World! This is a test.

{0} and {1} are placeholder tokens that resolve to the second argument and third argument to the FORMAT function respectively. So {0} resolves to World, provided by view.properties.title.

You can also bind a view to a data provider using the record key. This example uses the record_detail_message.view view from the sample app.

The view has one data provider with the record key. The data provider determines the structure of the returned data. This data provider is backed by UI API.

The view has one input property, recordId. To use the value of recordId in the view, build an expression using view.properties.

The view definition looks like this.