--- title: Anatomy of a complex query using extensions slug: '1464' canonical_url: https://docs.coveo.com/en/1464/ collection: build-a-search-ui source_format: adoc --- # Anatomy of a complex query using extensions You can obtain insightful contextual results using nested [queries](https://docs.coveo.com/en/231/) on many lines using [query extensions](https://docs.coveo.com/en/1397/). [Coveo for Salesforce](https://docs.coveo.com/en/1243/) takes advantage of such complex [queries](https://docs.coveo.com/en/231/) to present valuable contextual information in Coveo [Insight Panels](https://docs.coveo.com/en/2898/). You first need to know that in a multi-line [query](https://docs.coveo.com/en/231/), the lines are processed from the first to the last. The result set from the first line is available to the next lines and so forth. > **Leading practice** > > [Use aliases](https://docs.coveo.com/en/1465#use-aliases) to split the [query](https://docs.coveo.com/en/231/) into chunks that are easier to understand, and then use one or more aliases as building blocks on subsequent lines to nest [queries](https://docs.coveo.com/en/231/). A minimum knowledge of [standard query extensions](https://docs.coveo.com/en/1462/) and of the content of your [index](https://docs.coveo.com/en/204/) and Salesforce [fields](https://docs.coveo.com/en/200/) will help you understand existing complex [queries](https://docs.coveo.com/en/231/) and imagine new ones. The following example presents how a complex query can be used to present recent emails in a Coveo Insight Panel: [source,javascript,subs="attributes+"] ``` pass:[{{accounts=$type(name:'Account') @title="{!>Name}"}}] {{contacts=$join(fromResultSet: {{accounts}}, toResultSet: $type(name: 'Contact'), field: '@sfaccountid')}} {{emailAddresses=$valuesOfField(field: '@sfemail', resultSet: {{contacts}})}} $emailsExchangedWithThoseAddresses(addresses: {{emailAddresses}}) NOT @isattachment $sort(criteria: 'datedescending') ``` In short, this [query](https://docs.coveo.com/en/231/) lists decreasingly recent email messages exchanged with people connected to the account currently selected in Salesforce. ## Line 1 [source,javascript,subs="attributes+"] ``` pass:[{{accounts=$type(name:'Account') @title="{!>Name}"}}] ``` Line 1 creates the `accounts` [alias](https://docs.coveo.com/en/176/) that will only contain search results of type `Account` matching the currently selected Salesforce account. In more detail, the standard extension `$type` specifies that the result set of the [alias](https://docs.coveo.com/en/176/) contains [items](https://docs.coveo.com/en/210/) of type `Account`. The [field](https://docs.coveo.com/en/200/) query `@title="{!>Name}"` filters only accounts for which the content of the `title` [index](https://docs.coveo.com/en/204/) [field](https://docs.coveo.com/en/200/) is equal to the name of the currently selected account in Salesforce. > **Note** > > In the [index](https://docs.coveo.com/en/204/), the name of a Salesforce account object is stored in the `title` field. > In the context of Salesforce, `{!>Name}` returns the name of the currently selected account. ## Line 2 ```javascript {{contacts=$join(fromResultSet: {{accounts}}, toResultSet: $type(name: 'Contact'), field: '@sfaccountid')}} ``` Line 2 creates the `contacts` [alias](https://docs.coveo.com/en/176/) that will contain all of the contacts connected with the accounts from Line 1. In more detail, the standard extension `$join` specifies the content of the [alias](https://docs.coveo.com/en/176/) by taking the [alias](https://docs.coveo.com/en/176/) of Line 1 as the input `(fromResultSet: {{accounts}})`, and returns [items](https://docs.coveo.com/en/210/) of type Contact `(toResultSet: $type(name: 'Contact'))` that have the same Salesforce account ID (`sfaccountid` field value) as [items](https://docs.coveo.com/en/210/) in the `fromResultSet`. ## Line 3 ```javascript {{emailAddresses=$valuesOfField(field: '@sfemail', resultSet: {{contacts}})}} ``` Line 3 creates the `emailAddresses` [alias](https://docs.coveo.com/en/176/) that will contain the list of email addresses corresponding to the list of contacts from Line 2. In more detail, the standard extension `$valuesOfField` specifies the content of the [alias](https://docs.coveo.com/en/176/) by building a list of Salesforce email addresses `(field: '@sfemail')` for the contacts in the `contacts` [alias](https://docs.coveo.com/en/176/) created on Line 2. ## Line 4 ```javascript $emailsExchangedWithThoseAddresses(addresses: {{emailAddresses}}) ``` Line 4 uses the `$emailsExchangedWithThoseAddresses` standard extension to get all of the emails exchanged with the list of email addresses from the `emailAddresses` [alias](https://docs.coveo.com/en/176/) created with Line 3. ## Line 5 ```javascript NOT @isattachment ``` Line 5 filters out the results from Line 4 that are attachments. In more detail, the line uses a Boolean operator (`NOT`) with a [field](https://docs.coveo.com/en/200/) query `@isattachment` to filter on the `isattachment` Boolean [index](https://docs.coveo.com/en/204/) [field](https://docs.coveo.com/en/200/). In the context of a Coveo [Insight Panel](https://docs.coveo.com/en/2898/), you usually only want the messages themselves. The user can always click a result to open the email in the original email application and review attachments in this context. ## Line 6 ```javascript $sort(criteria: 'datedescending') ``` Line 6 uses the `$sort` standard extension to sort email messages by inverse time values. ## What's next? Review examples of [query extensions](https://docs.coveo.com/en/1182/) that are specific to [Coveo for Salesforce](https://docs.coveo.com/en/1404/).