Skip to main content
2 of 2
Commonmark migration

Alias notation also works with the toLabel() function in both SOQL and SOSL queries.

The toLabel function supports aliasing. In addition, aliasing is required when the query includes the same field multiple times. For example:

FIND {Joe} RETURNING Lead(company, toLabel(Recordtype.Name) AliasName)

SOSL Reference: toLabel(fields)

The above excerpt, shows the use of the alias notation with toLabel function in a SOSL query, however, the same syntax works for SOQL queries.

For example, if we have the following Account records in Salesforce:

+--------------+--------------------+
| Name         | BillingCountryCode | 
+--------------+--------------------+
| A            | US                 |
| B            | IE                 |
| C            | CA                 |
+--------------+--------------------+

Without the toLabel alias we would only be able to select one of BillingCountryCode or toLabel(BillingCountryCode). If we tried to select both without an alias we would run into an error:

duplicate field selected: BillingCountryCode

Now, if we use alias notiation, we can query for both the Billing Country Code as well as the label as follows:

SELECT
    Name,
    BillingCountryCode,
    toLabel(BillingCountryCode) BillingCountryLabel
FROM Account

Then the above query would produce a query result as follows:

+--------------+--------------------+---------------------+
| Name         | BillingCountryCode | BillingCountryLabel | 
+--------------+--------------------+---------------------+
| A            | US                 | United States       |
| B            | IE                 | Ireland             |
| C            | CA                 | Canada              |
+--------------+--------------------+---------------------+
Alan Morey
  • 2.5k
  • 2
  • 24
  • 27