Don't execute useless queries with $query.abort()

 



Problem

Imagine that you have two fields Company and Contact

Company configuration (field id: 10001)

Query
SELECT id, name
FROM Companies
Key0  ( the id column of the result set)
Template{name}

Contact configuration

Query
SELECT id, name
FROM Contact
WHERE company_id = $issue.customfield_10001
Key0  ( the id column of the result set)
Template{name}


The Contact field depends on the Company field but doesn't make sense if Company is not set.

The Contact field query is executed every time, even if the Company is not set. This will certainly return an empty list and nothing will be displayed in the field.

This is fine from the end user's point of view, but it would be much better if we could avoid sending a useless query to the datasource. To do this, you need to check if the parent field is empty and call $query.abort() to cancel the query execution.

Solution

In our example, the query should be changed as follows:

#if(!$issue.customfield_10001 || $issue.customfield_10001.length() == 0)
    $query.abort()
#end
SELECT id, name
FROM Contact
WHERE company_id = $issue.customfield_10001

So, when the Company field (customfield_10001) is empty, $query.abort() is executed to cancel the query execution.

This works exactly as if the query returned no result, but the useless query to the datasource is avoided.

This is particularly helpful when you have a lot of dependent fields.

(info) For more information on this topic, please read the following documentation: How to use variables in a Connect field query