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:

  1. Log in to JIRA as an administrator.
  2. Go to the Administration icon  and click on it.
  3. Click on Issues - > Custom fields.
  4. Click on Add custom field.
  5. Click on Advanced in the left panel.
  6. Locate Calculated Number Field and select it.
  7. Click on Next.
  8. Provide a name for the custom field.
  9. You can optionally provide a description for the custom field.
  10. Click on Create.
  11. Associate the custom field to the appropriate screens.
  12. Locate the custom field in the Custom fields administration page and click on the cog wheel.
  13. Click on Configure and create a formula (78318099) for the field.
  14. You can create multiple contexts if you need to associate different formulas with particular projects or issue types.
  15. 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 rangeThe 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 gadgetsSee 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;

  1. Locate the custom field on the Custom Fields administration page.
  2. Click on the cog wheel and click on Configure
  3. Click on Edit Groovy Formula
  4. 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.
  5. Click on Save.

As documented on the Expected Value tab of the Groovy editor helpthe 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;

  1. Locate the custom field on the Custom Fields administration page.
  2. Click on the cog wheel and click on Configure
  3. Click on Edit Format Expression.

  4. In the editor write a Groovy script to format the number returned by the Groovy formula (78318099). 
  5. You can test you written script against any issue using the Groovy script tester.
  6. 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;

  1. Locate the custom field on the Custom Fields administration page.
  2. Click on the cog wheel and click on Configure
  3. Click on Edit Velocity Template.
  4. Input the template.
  5. 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;

  1. Locate the custom field on the Custom Fields administration page.
  2. Click on the cog wheel and click on Configure
  3. Click on Edit List View Velocity Template.
  4. Input the template.
  5. 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