How to use variables in a Connect field query


Table of content

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.

(info) 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

(info) 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:

VariableTypeHow to check itExample
SummaryStringlength()$issue.summary.length()
AssigneeUsersize()$issue.assignee.size()
Assignee attributeStringlength()$issue.assignee.displayName.length()
Custom Text FieldStringlength()$issue.customfield_xxxxx.length()
Custom Select ListObjectsize()$issue.customfield_xxxxx.size()
Connect Field - Live Text (single value)Stringlength()$issue.customfield_xxxxx.length()
Connect Field - Live Text (multi value)Objectsize()$issue.customfield_xxxxx.size()
Connect Field - Live User (single & multi value)Usersize()$issue.customfield_xxxxx.size()
Connect Field - Snapshot (Text, Date & Datetime)Stringlength()$issue.customfield_xxxxx.length()
$userInputStringlength()$userInput.length()
$currentCustomfieldValuedepends 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()