A Calculated Number custom field type represents a number and is displayed in Double format. It is a read-only field. |
To add a Calculated Number custom field type to your instance:
Issues - > Custom fields.
Add custom field
.Advanced
in the left panel.Calculated Number Field
and select it.Next
.Create.
If you have upgraded from JMCF |
When creating a Calculated Number custom field type, you can configure the Search template as Number range searcher
or Number range searcher (Statistics-compatible)
to be able to search issues within a number range. Selecting the Number range searcher (Statistics-compatible)
searcher also allows calculated number fields to be used in statistics gadgets. If you select “None” for the searcher, then you won’t see any reference to this field in the Issue Navigator search fields. Note that changing a custom field searcher requires a re-index.
Starting from 2.0.0 BeanShell has been replaced by Groovy in the JMCF add-on. Scripts written in BeanShell will work unchanged in Groovy except for some specific cases. |
To write a formula for the Calculated Number custom field type;
Configure
Edit Groovy Formula
Save.
As documented on the Expected Value tab of the Groovy editor help, the Groovy formula must return a number (Integer, Long or Double) or null
.
Examples of Groovy formula for a Calculated Number custom field:
Simple Math calculation such as,
2+3 |
A Groovy expression to reference any field of the issue that returns an Integer or float such as,
issue.get("Story Points") |
A Groovy expression that includes arithmetic operators as well as any other Groovy operator, and Groovy method calls
issue.get("Story Points") * 5 |
issue.get("Affects Version/s").size() |
A Groovy expression that adds two custom fields, such as "Business Value" and "Technical Value" to get an "Overall Value":
issue.get("customfield_10114") ?: 0 + issue.get("customfield_10150") ?: 0 |
A String representing a number returned either by a constant or a Groovy expression. The String is converted to a Double.
issue.getKey().substring(5) |
By default, the Calculated Number custom field type value is displayed using Jira's default number format (if you leave the Number Format Expression
blank). However, you can customize it using Groovy. To configure a format for a Calculated Number custom field type;
Configure
Click on Edit Format Expression.
Save.
The Groovy script must return a String containing HTML markup.
Example 1
Consider an example where you want to calculate the amount to be paid in dollars for the Time Spent on the issue. For this, you will need to first calculate the amount to be paid and then format it using the numberTool to display it as a currency.
Groovy formula:
(issue.get("timespent")/60/60) * 20 |
Groovy format:
numberTool.format('currency',value) |
If the Time Spent on the issue is 7200
seconds, the Groovy expression will return the number 40
and it will be formatted to $40.
Example 2
Consider another example where you want to display an icon to the left of the field value depending on the field value. For this, you will need to format the value returned by the Groovy formula using the numberTool and add an IMG tag to display an icon to the left of the number.
Groovy 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); |
If the Groovy formula returns 23, the calculated field is displayed as
You can define a velocity template that will return the Html representation of the calculated custom field. Leave it empty to use the default template.
To write a velocity template for a Calculated Number custom field type value;
Configure
Edit Velocity Template
.Save.
You can use the following Velocity variables in the template:
$value
: the raw field value, a number in Integer or Float format.$formattedValue
: the field value formatted according to the field configuration aboveFor example:
<div style="border: dashed"> $formattedValue </div> |
displays the formatted value of the field with a dashed border.
The Never show on Transition and Edit screens
option controls whether the calculated Duration custom field can appear on transition and edit screens. If you select this option, the field will not be visible on any Edit or Transition screen, even if it is added to the screen.
You can access this field using any of the following getters of the Issue interface
get("Your custom field name") or get("customfield_xxxxx")
that returns a number in a Double format.
getAsString("Your custom field name")
or
getAsString("customfield_xxxxx")
that returns a String
representing a number.