Our new Appfire Documentation Space is now live!
Take a look here! If you have any questions please email support@appfire.com
Calculated (Scripted) Number custom field type
A Calculated Number custom field type represents a number and is displayed in Double format. It is a read-only field.
Adding a Calculated Number custom field type
To add a Calculated Number custom field type to your instance:
- Log in to JIRA as an administrator.
- Go to the Administration icon and click on it.
- Click on
Issues - > Custom fields.
- Click on
Add custom field
. - Click on
Advanced
in the left panel. - Locate
Calculated Number Field
and select it. - Click on
Next
. - Provide a name for the custom field.
- You can optionally provide a description for the custom field.
- Click on
Create.
- Associate the custom field to the appropriate screens.
- Locate the custom field in the Custom fields administration page and click on the cog wheel.
- Click on Configure and create a formula (explained below) for the field.
- You can create multiple contexts if you need to associate different formulas with particular projects or issue types.
- Perform a re-index as prompted by JIRA.
If you have upgraded from JMCF 1.x
to 2.x
please refer to the upgrade guide.
Search template
When creating a Calculated Number custom field type, you can configure the Search template as
- Number range searcher - Allow searching for a number that is in a given range
- Number range searcher (Statistics-compatible) - Allow searching for a number that is in a given range. Supports Statistics Gadgets.
Using these searchers you will be able to search issues within a number range. The default search template is Number range searcher (Statistics-compatible). 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.
Number range searcher (Statistics-compatible) is a custom statistics compatible searcher provided by JMCF that allows Number type custom fields of Jira and JMCF to be used in the statistics gadgets. See here for more information.
Configuring the Groovy formula for a Calculated Number custom field
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;
- Locate the custom field on the Custom Fields administration page.
- Click on the cog wheel and click on
Configure
- Click on
Edit Groovy Formula
- In the editor write a Groovy script that returns a number. Also, you can test your script against any issue using the Groovy script tester.
- Click on
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)
Customizing the formatting of the Calculated Number custom field type value
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;
- Locate the custom field on the Custom Fields administration page.
- Click on the cog wheel and click on
Configure
Click on
Edit Format Expression.
- In the editor write a Groovy script to format the number returned by the Groovy formula (see above).
- You can test you written script against any issue using the Groovy script tester.
- Click on
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
Customizing the display of the Calculated Number custom field type value on the issue detail view
You can define a velocity template that will return the Html representation of the calculated custom field on the issue detail view. Leave it empty to use the default template.
To write a velocity template for a Calculated Number custom field type value;
- Locate the custom field on the Custom Fields administration page.
- Click on the cog wheel and click on
Configure
- Click on
Edit Velocity Template
. - Input the template.
- Click on
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 above- and other variables described on this page
For example:
<div style="border: dashed"> $formattedValue </div>
displays the formatted value of the field with a dashed border.
Customizing the display of the Calculated Number custom field type value on the search list view
You can define a velocity template that will return the Html representation of the calculated custom field on the search list view. Leave it empty to use the default template.
To write a velocity template for a Calculated Number custom field type value;
- Locate the custom field on the Custom Fields administration page.
- Click on the cog wheel and click on
Configure
- Click on
Edit List View Velocity Template
. - Input the template.
- Click on
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 above- and other variables described on this page
For example:
<div style="border: dashed"> $formattedValue </div>
displays the formatted value of the field with a dashed border.
Displaying the Calculated Number custom field type value on Transition and Edit screen
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.
Accessing the Calculated Number custom field value from other Groovy scripts
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 aString
representing a number.