Guide To Writing Queries

Fields & Links

12min

TL;DR

  • To reference a field write e.g. Description. If a field contains spaces or other special characters, write e.g. [Billing City]
  • To reference a field of a related module, write e.g. Calls.Subject
  • Use <all!> before a link name to specify "all and at least one" records. For example, typing <all!>Calls. [Start Date] = tomorrow in Accounts will find all Accounts that have at least one related Call, and all their related calls start tomorrow.
  • Use <all?> to specify "all or none". For example, typing <all?>Calls. [Start Date] = tomorrow in Accounts will find all Accounts that either have no related Calls, or if they do, they all start tomorrow.
  • Use <some> to specify "one or more". For example, typing <some>Calls. [Start Date] = tomorrow in Accounts will find all Accounts that have one or more related Calls that start tomorrow. The <some> can be omited, so typing Calls. [Start Date] = tomorrow gives the same result

Field Names

A field is referenced by its name. If the name contains spaces or other special characters, enclose it in square brackets [ ]:

Document image


If for some strange reason, the character ] is part of the actual name of the field, precede it with a backslash \.

Document image

Document image


Both system names and labels can be used:

Document image


When auto-filling from a hint, QuickQuery uses the label variant if it's available, and automatically adds brackets and backslashes if they're needed.

Fields in Linked Records

To reference a field in a related module, write the name of the link, followed by a period ., and then the name of the field. Link names follow the same rules as stated above (square brackets when special characters are involved etc.).

Document image



Quantifiers

The query above searched for Accounts that have some (i.e. at least one) Opportunity having Likely > $25k. However, you could easily want to search for Accounts where all Opportunities have Likely > $25k. This is what quantifiers are for.

Accounts where all Opportunities are expected to close the month after next, or later
Accounts where all Opportunities are expected to close the month after next, or later


Quantifiers are written before the link name, and enclosed in < and >. There are three quantifiers in QuickQuery:

  • <some> - at least one linked record satisfies the condition
  • <all?> - all linked records satisfy the conditions or no linked records exist
  • <all!> - all linked records satisfy the condition, and at least one linked record exists

If you omit the quantifier, it is interpreted as <some>.

Difference between all! and all?

The only difference between <all!> and <all?> is how they treat situations where there are no linked records - <all!> excludes those records, while <all?> includes them. Think of the question mark ❓ as a "maybe exists?", while the exclamation mark ❗as a "must exist!"

To understand why this is useful, imagine you want to find out which Accounts haven't been called in the past month - in other words, all Calls are strictly before last month.

<all!/all?>Calls.[Start Date] strictly before last month

The question is, what quantifier should you use - <all?> or <all!>? Well, let's think about it - what if an Account had no calls at all, ever? That would certainly qualify as the type of Account you would want to see on your list, wouldn't it? And that's precisely why <all?> is there for you to use.

Contrast that with the query on the picture above, which looks for Accounts where all Opportunities are expected to close the month after next, or later. In that situation, it is obvious that you don't want to see Accounts that have no Opportunities - it makes no sense. And that's why you would use <all!>.

The only thing you need to remember is this: when searching by related fields, always think about how you want to treat situations where no linked records exist. Do you want to include them? Use <all?>. Do you want to exclude them? Use <all!>.

Negating quantifiers

Let's take a look at the following two queries (we're in Accounts):

Tasks.Description not empty
not (Tasks.Description empty)

Obviously, both are somehow asking for Accounts where the Task Description isn't empty. However, there is a difference between the two.

The first query is easy to understand - we didn't include a quantifier, so it's <some>, which means that we're looking at Accounts where at least one Task doesn't have an empty Description.

The second query is a little trickier. The inside part denotes Accounts where at least one Task does have an empty Description. This is wrapped in not ( ), which means it's the opposite. However, the "oppositeness" doesn't only apply to the empty part, it also applies to the <some> part!

To figure out what the opposite of <some> is, it is useful to translate it to its technical meaning, which is "exists". In other words, the inside query is "a Task with an empty Description exists". The opposite of that is "a Task with an empty Description doesn't exist". And if such a Task doesn't exist, it means that either all linked Tasks have non-empty Descriptions, or no linked Tasks exist at all.

Whew! 🥵 Math-class-PTSD flashbacks anyone? Yeah, us too. That's why we prepared the table bellow, where you can simply look up the negation of a given quantifier if you ever need it, and be done with it. You can find some examples bellow as well.



Quantifier

Technical meaning

Negation

Negated quantifier

<some>X

At least one linked record exists that satisfies X

Either all linked records satisfy notX or no linked records exist

<all?>not(X)

<all?>X

Every linked record satisfies X or no linked records exist

At least one linked record satisfies notX

<some>not(X)

<all!>X

Every linked record satisfies X

Either at least one linked record satisfies notX or no linked records exist

<some>not(X) or "empty" (see example) bellow)



Example for <some>

The following two queries are equivalent

Document image

Document image




Example for <all?>

The following two queries are equivalent

Document image

Document image


Example for <all!>

The following two queries are equivalent

Document image

Document image


Quantifier ordering

When using quantifiers, their order begins to matter. Take the following two queries for instance (we're in Accounts):

Document image

Document image


At first glance, we might feel that both should give the same result, but this is not the case. The reason for this can be seen when you read those queries out loud.

The query on the left is saying "Accounts where every Opportunity ID equals some Opportunity ID under that Account". Well, when you think about it, that's always the case! Naturally, every Opportunity ID is equal to itself, so there will always be at least one Opportunity ID under that Account which will match!

However, the query on the right is saying "Accounts where some Opportunity ID equals every Opportunity ID", which is a very different beast - it means that one ID must be the same as all the other ones, which basically means that they must all be the same! Since IDs are always unique, this can only occur when there is a single record, since that is the only situation where "one equals all".

Updated 25 Mar 2024
Doc contributor
Did this page help you?