Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Better documentation for calculated fields

...

Formula syntax

The formula is written using the BeanShell language, and should be included in the field description inside an html comment (so that it remains invisible when showing the field description), preceded by the following keyword: @@Formula: (note the colon at the end). For example:

...

Code Block
issue.get("<field_ID_or_ID>Name>")

where <field_ID_or_ID> Name> is either a built-in JIRA field ID or a or custom field ID (the latter in the form customfield_nnnnn), or a field name. See this page for details.

...

Info
titleDebugging the script

If the calculated field does not show up on the view issue screen, you must make sure the formula isn't raising an error. For that, you must look at the end of the JIRA log file (atliassian-jira.log) right after displaying the issue (displaying an issue will always force calculated fields to be re-calculated).

Example
Adding two fields

To add two custom fields, such as "Business Value" and "Technical Value" to get an "Overall Value":

Code Block
<!

You can also include logging code into your script using the "log" object. For example:

Code Block
<!-- @@Formula: 
(issue
log.
get
error("
customfield_10114") != null ?
Value of field 10114 is: "+issue.get("customfield_10114")
: 0
);
+ (issue.get("customfield
log.error("Value of field 10150 is: "+issue.get("customfield_10150"))
!
;

if (issue.get("customfield_10114") == null 
?
|| issue.get("customfield_10150") 
:
== 
0
null)
-->
Custom formatting

You can also specify custom formatting for the value of the Calculated Number field. In the Description field, add your formatting formula using the following syntax:

Code Block
<!-- @@Format: formula goes here -->

The formula itself is a Java-style expression that can reference the value returned by the formula using the value variable. You can also use the numberTool object to format the number value:

Code Block
numberTool.format(value)
Example

...


    return null;
 
return issue.get("customfield_10114") + issue.get("customfield_10150");   //we already made sure neither field is empty (==null) 
-->

We recommend you write to the logs using the "log.error()" method because ERROR-level logs are always written out to atlassian-jira.log (whereas DEBUG-level logs are filtered out by default).

Example
Adding two fields

To add two custom fields, such as "Business Value" and "Technical Value" to get an "Overall Value":

Code Block
<!-- @@Format:
if (value > 21)
  return "<img src='/images/icons/priority_trivial.gif'> "+numberTool.format(value);
else if (value >= 10)
  return "<img src='/images/icons/priority_major.gif'> "+numberTool.format(value);
else
  return "<img src='/images/icons/priority_blocker.gif'> "+@@Formula: (issue.get("customfield_10114") != null ? issue.get("customfield_10114") : 0) + (issue.get("customfield_10150") != null ? issue.get("customfield_10150") : 0) -->
Custom formatting

You can also specify custom formatting for the value of the Calculated Number field. In the Description field, add your formatting formula using the following syntax:

Code Block
<!-- @@Format: formula goes here -->

The formula itself is a Java-style expression that can reference the value returned by the formula using the value variable. You can also use the numberTool object to format the number value:

Code Block
numberTool.format(value);
 -->

...

  1. Create a new custom field, choosing "Calculated Text Field" for its type.
  2. During step 2, in the Description field, include the calculation formula using the syntax described below.
  3. Re-index your data, as JIRA will kindly suggest you to.
Formula syntax

The formula should be included in the field description inside an html comment (so that it remains invisible when showing the field description), preceded by the following keyword: @@Formula: (note the colon at the end). For example:

Code Block
<!-- @@Formula: formula goes here -->

You can naturally include the real description of the custom field as well. For example:

Code Block
This field represents the first 10 characters of the issue's summary field.
<!-- @@Formula: issue.get("summary").substring(0,10) -->

The formula itself is a Java-style expression that can reference any issue field value, include Java operators, and Java method calls (such as the .substring(0,10) example above). Access to issue field values is achieved by the following syntax:

Code Block
issue.get("<field_ID>")

where <field_ID> is a built-in JIRA field ID or a custom field ID (in the form customfield_nnnnn). See this page for details.

You can also access the JIRA Issue object using the issueObject variable.

Info
titleTo identify the custom field ID:
  1. go to Administration/Custom Fields
  2. click on the "Configure" link for the custom field you're interested in
  3. in the URL of the Configure Custom Field page, note the number after "customFieldId=" and append it to "customfield_" to build the custom field ID

Note that you must make sure the formula returns a String, or null.

Info
titleDebugging the script

If the calculated field does not show up on the view issue screen, you must make sure the formula isn't raising an error. For that, you must look at the end of the JIRA log file (atliassian-jira.log) right after displaying the issue (displaying an issue will always force calculated fields to be re-calculated).

Exact Text Searcher (Statistics-compatible) (new in 1.5.6)

When creating a Calculated Text Field, you can choose between the standard Free Text Searcher and the new Exact Text Searcher (Statistics-compatible). The latter should be used if you need either of the following:

  • Be able to search issues based on the exact value of the custom field (i.e. use the "=" operator instead of the "~" operator)
  • Be able to use the field in one of the statistics reports or dashboard gadgets

 

...

  1. Create a new custom field, choosing "Calculated Date/Time Field" for its type.
  2. During step 2, in the Description field, include the calculation formula using the syntax described below.
  3. Re-index your data, as JIRA will kindly suggest you to.
Formula syntax

The formula should be included in the field description inside an html comment (so that it remains invisible when showing the field description), preceded by the following keyword: @@Formula: (note the colon at the end). For example:

Code Block
<!-- @@Formula: formula goes here -->

You can naturally include the real description of the custom field as well. For example:

Code Block
This field represents 10 days after the creation of the issue.
<!-- @@Formula: org.apache.commons.lang.time.DateUtils.addDays(issue.get("created"),10) -->

The formula itself is a Java-style expression that can reference any issue field value, include Java operators, and Java method calls (such as the org.apache.commons.lang.time.DateUtils.addDays() example above). Access to issue field values is achieved by the following syntax:

Code Block
issue.get("<field_ID>")

where <field_ID> is a built-in JIRA field ID or a custom field ID (in the form customfield_nnnnn). See this page for details.

You can also access the JIRA Issue object using the issueObject variable.

Info
titleTo identify the custom field ID:
  1. go to Administration/Custom Fields
  2. click on the "Configure" link for the custom field you're interested in
  3. in the URL of the Configure Custom Field page, note the number after "customFieldId=" and append it to "customfield_" to build the custom field ID

Note that you must make sure the formula returns a java.util.Date, or null.

...

titleDebugging the script

...


Example

To display an icon to the left of the field value depending on the field value:

Code Block
<!-- @@Format:
if (value > 21)
  return "<img src='/images/icons/priority_trivial.gif'> "+numberTool.format(value);
else if (value >= 10)
  return "<img src='/images/icons/priority_major.gif'> "+numberTool.format(value);
else
  return "<img src='/images/icons/priority_blocker.gif'> "+numberTool.format(value);
 -->

Anchor
calculatedtextfield
calculatedtextfield
Calculated Text Field (new in 1.5.5)

  1. Create a new custom field, choosing "Calculated Text Field" for its type.
  2. During step 2, in the Description field, include the calculation formula using the syntax described below.
  3. Re-index your data, as JIRA will kindly suggest you to.
Formula syntax

The formula is written using the BeanShell language, and should be included in the field description inside an html comment (so that it remains invisible when showing the field description), preceded by the following keyword: @@Formula: (note the colon at the end). See Calculated Number Field above for details.

Note that you must make sure the formula returns a String, or null.

Exact Text Searcher (Statistics-compatible) (new in 1.5.6)

When creating a Calculated Text Field, you can choose between the standard Free Text Searcher and the new Exact Text Searcher (Statistics-compatible). The latter should be used if you need either of the following:

  • Be able to search issues based on the exact value of the custom field (i.e. use the "=" operator instead of the "~" operator)
  • Be able to use the field in one of the statistics reports or dashboard gadgets

 

Anchor
calculateddatefield
calculateddatefield
Calculated Date/Time Field (new in 1.5.5)

  1. Create a new custom field, choosing "Calculated Date/Time Field" for its type.
  2. During step 2, in the Description field, include the calculation formula using the syntax described below.
  3. Re-index your data, as JIRA will kindly suggest you to.
Formula syntax

The formula is written using the BeanShell language, and should be included in the field description inside an html comment (so that it remains invisible when showing the field description), preceded by the following keyword: @@Formula: (note the colon at the end). See Calculated Number Field above for details.

Note that you must make sure the formula returns a java.util.Date, or null.

Custom formatting

You can also specify a date/time format for the value of the Calculated Date/Time field. In the Description field, add the name of the date/time format using the following syntax:

Code Block
<!-- @@Format: <format_name> -->

where <format_name> is one of the constants found here: https://developer.atlassian.com/static/javadoc/jira/reference/com/atlassian/jira/datetime/DateTimeStyle.html

...