Excerpt |
---|
A calculated date time custom field type returning a |
Adding a calculated date/time custom field
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 Date/Time custom field type
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.
Panel | ||
---|---|---|
| ||
|
Panel | ||
---|---|---|
| ||
Note |
---|
If you have upgraded from JMCF |
Search template:
When creating a calculated date/time custom field type, you can configure the Search template as Date range picker
to be able to search issues within a date/time range of values for that custom field. 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.
Configuring the Groovy formula for a calculated date/time type custom field
Note |
---|
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 custom field;
- 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
java.util.Date
. You can test you written script against any issue using the Groovy script tester. - Click on
Save.
Some examples of a Groovy formula:
A simple String representing a timestamp in the format specified in your Jira system settings. For example:
Code Block language groovy linenumbers true "11/Apr/17"
A Groovy expression that returns a
java.util.Date
Code Block language groovy linenumbers true new Date()
A Groovy expression to reference any field of the issue that returns a
Timestamp
Code Block language groovy linenumbers true issue.get("created")
A Groovy expression that includes arithmetic operators as well as any other Groovy operator, and Groovy method calls
Code Block language groovy linenumbers true issue.get("created") + 5
Code Block language groovy linenumbers true issue.get("duedate").format("YYYY/MM/DD")
Note |
---|
Make sure that the formula returns either a TimeStamp or a String representing a TimeStamp or |
Customizing the display of the calculated date/time custom field type value
By default, the calculated date/time field value is displayed using Jira's default date format (if you leave the Edit date format
to Default
). However, you can customize it using Groovy. To configure a format for a calculated date/time 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 value 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:
Code Block | ||||
---|---|---|---|---|
| ||||
(issue.get("timespent") / 60 / 60) * 20 |
Groovy format:
Code Block | ||||
---|---|---|---|---|
| ||||
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:
Code Block | ||||
---|---|---|---|---|
| ||||
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