It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Calculate original estimation field using behaviours script runner



I want to calculate automatically the original estimation based on 2 dates ,

I used behaviours plugin and it is not working.


import com.onresolve.jira.groovy.user.FormField
import com.onresolve.jira.groovy.user.FieldBehaviours
// Get a pointer to the Start Date Field
def EndDateField = getFieldByName('Ends On')

def OriginalEstimate=getFieldByName('Original Estimate')

def StartDatefield =getFieldByName('Starts On')
def StartDateVal= (Date) StartDatefield.getValue()
def EndDateVal=(Date) EndDateField.getValue()
log.debug("StartDateVal Value "+ StartDateVal.format("yyy-MM-dd") )
log.debug("EndDay Value "+ EndDateVal.format("yyy-MM-dd") )
def Enddateobject=new Date(EndDateVal.format("yyy-MM-dd"))
def startdateobject=new Date(StartDateVal.format("yyy-MM-dd"))
def daysBetweenDates=Enddateobject-startdateobject+1
log.debug("the daysBetweenDates is "+ daysBetweenDates )
def longEstimate2 = daysBetweenDates * 28800.toLong();
log.debug("the estimation is "+ longEstimate2 )

it does not calculate the daysbetweenDates.

@Kristian Walker (Adaptavist) please help.


2 answers

1 accepted

2 votes
Answer accepted

Hi Rita,

I have had a chance to look at this for you this afternoon on my JIRA 7.10 instance using Script Runner version

I have managed to get this work by using the code below which I placed as a behaviour on the Ends On field.

Notice in order to get this to work you need to reference the original estimate by its ID. Also the calculation for the Long value is multiplying the number of days by 480 minutes in order to get the number of days. This is due to the fact that by default JIRA is configured to think a day is 8 hours. 

If you have configured your JIRA differently or want to multiply by a differnt amount then you will need to change the calculation line of def longEstimate2 = daysBetweenDates * 480 as Long to use the calculation that your require.

Also simply substracting start date from end date will include weekends so I have modified the example below to only count the number of weekdays between 2 dates. If you wish to include weekends you can exclude the code for calculating weekdays and replace it with the line def daysBetweenDates=Enddateobject-startdateobject+1 from your code above.

import java.text.DateFormat;
import java.util.Calendar;

// Get a pointer to my custom fields
def startDate = getFieldByName("Starts On")
def endDate = getFieldByName("Ends On")
def originalEstimate = getFieldById("timetracking_originalestimate")

// Get the Values of My Date Fields
def startDateVal = startDate.getValue() as Date
def endDateVal = endDate.getValue() as Date
// Specify a variable used for storing the number of weekdays between two dates
int daysBetween;

// Check if End Date contains a valid value
if (endDateVal) {

 // Calculate the number of weekdays between the start and end date below

 Calendar cal1 = Calendar.getInstance();
 Calendar cal2 = Calendar.getInstance();
 cal2.setTime(endDateVal != null ? endDateVal : startDateVal);

 if (cal2.before(cal1)) {
 // Don't attempt to calculate days if end date is before the start date
 return 0;

 while (cal1.before(cal2)) {
 // If the days include weekends skip those days and do not count them
 if ((Calendar.SATURDAY != cal1.get(Calendar.DAY_OF_WEEK))
 && (Calendar.SUNDAY != cal1.get(Calendar.DAY_OF_WEEK))) {

 cal1.add(Calendar.DATE, 1);

// End of code block for calculating the number of weekdays

 // Work out the long value by multiplying the number of days against the number of hours in our work day
 // that we have configured inside JIRA e.g 4 * 480 = L1920 which represents 4 days 
 def longEstimate2 = daysBetween * 480 + 480 as Long // Plus 480 to add on the day that the current day that the date calculation chops off.
 // Set the original estimate here

I hope this helps.



Hello @Kristian Walker (Adaptavist)


I have only one problem, that when I change the date , it didn't change it at the moment .I have to save then edit to take place.

Thanks for your help



Hi Rita,

The behaviour needs to be configured on the Ends On field as per the attached screenshot here so it only fires when the Ends On date is updated.

Also behaviours only work on the create and edit screen for issues. This means that it will not work when fields are updated on the view screen and by default newer versions of script runner disable inline editing for fields when a behaviour is placed on them.

For existing issues which you wish to set the value on then you will need to simply edit these fields and ensure the Starts on and Ends on fields are set with values.

I hope this helps.



Hi @Kristian Walker (Adaptavist)

I was changing the ends on of the issue but I have to save the issue than edit it to take effect.

It does not take it automatically.when I change the end date.

awaiting your feedback.


Yes. This is the expected behaviour as the issues can only be edited via a screen. As mentioned in the last comment issues cannot be edited via inline editing as behaviours does not support this.

Once the Starts On and End on's fields are updated then this will add the value to the original estimate field as the minutes value and you will see this once the issue is saved and the result will show on the view issue screen.

If this is working as expected can you please accept this answer so that other users can easily find it. I have attached the document here which shows how the field works in my JIRA 7 instance. Note i need all three fields to be on the create and view screen.

If you have further questions can you please share some screenshots in a word doucument using a website such as tempsend or imggur as answers does not seem to be allowing images to be uploaded at pressent.

I hope this helps.



@Kristian Walker (Adaptavist) how can I check if the value of Ebd on have been changed?

or the starts on Value changed to recalculate it in the edit mode?

Hi Rita,

I am unsure of what you mean by EBD.

If you wanted to the behaviour to fire when the Start Field is updated then you would add this to the Starts On field instead of the end date field.

The easiest way would be to simply add the built in required behaviour on bot fields in order to make them required.

That way you could always ensure both fields are filled in.



Hi @Kristian Walker (Adaptavist)

I have put the behaviours on the remaining estimate field .

I will move it now to Ends on



Can you help me? How can I check for the originalEstimate.sum() to be greater than 0 for an issue to be moved into in progress?

Hi Rita,

Could I ask for a bit further information here.

I can see that you are looking to get the value of 2 fields called "Ends On" and 'Starts On'.

Would you simply just like to calculate the number of Days between these fields when the End's on field is updated and set it as the value in the original estimate field?




Hi @Kristian Walker (Adaptavist)

Starts on and ends on are dates field.

what i'm trying to do is to calculate the estimation field based on those 2 days.

For example when start date is 2016-03-08 and the ends on is 2016-03-09 i should have the original estimation  2

i hope that i have clarify the idea.

thank you

Hi Rita,

This makes sense. I will look into how this can be done for you and will aim to provide you an answer back within the next couple of days.



Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Marketplace Apps & Integrations

Marketplace Spotlight: November Continuous Integrations

                                                Shipping new code shoul...

66 views 0 1
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you