How to use variables in a Connect field query
Purpose
It is a good practice to optimize your queries by checking variables' contents. If a variable is empty, the query may no longer be valid and relevant and you should prevent its execution.
This improves performance and efficiency, and also reduce network exchanges.
For more performance tips, please read the following documentation: Improve performances
Working principle
When a variable is used in a query (such as $issue.summary, $issue.project.id, etc...), you should always apply the following logic:
#if($issue.summary.length() > 0)
YOUR_QUERY
#else
$query.abort()
#end
In this example, we check if the "$issue.summary" variable contains a value:
- If yes, then we execute the query as expected
- If not, then we execute $query.abort() to cancel the query execution
Depending on the type of variable to be checked, either length() or size() should be used.
When should you use "size()" or "length()"?
When the variable to be checked is just a string (such as the "summary" field, the "description" field, a basic text field, etc.), you should use length(). But if the variable to be checked is more complex (such as a user field or a multi-value field), size() should be used instead.
Here is a non-exhaustive list:
Variable | Type | How to check it | Example |
---|---|---|---|
Summary | String | length() | $issue.summary.length() |
Assignee | User | size() | $issue.assignee.size() |
Assignee attribute | String | length() | $issue.assignee.displayName.length() |
Custom Text Field | String | length() | $issue.customfield_xxxxx.length() |
Custom Select List | Object | size() | $issue.customfield_xxxxx.size() |
Connect Field - Live Text (single value) | String | length() | $issue.customfield_xxxxx.length() |
Connect Field - Live Text (multi value) | Object | size() | $issue.customfield_xxxxx.size() |
Connect Field - Live User (single & multi value) | User | size() | $issue.customfield_xxxxx.size() |
Connect Field - Snapshot (Text, Date & Datetime) | String | length() | $issue.customfield_xxxxx.length() |
$userInput | String | length() | $userInput.length() |
$currentCustomfieldValue | depends on the field type (see below) |
The $currentCustomfieldValue variable corresponds to the value stored in the Connect field itself. Thus, the way to check it depends on its type:
- If $currentCustomfieldValue is used in a "Live Text field (single-value)" or a "Snapshot" field, use length()
- If $currentCustomfieldValue is used in a "Live User" or "Live Text (multi-value)" field, use size()