Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

calculate the remaining estimated based on dates

Rita YOUNES [Infosysta]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 16, 2016

Hello, 

I want to automatically calculate remaining estimated based on start date and end date.

For example when I create an issue I will add start date :2016-02-17 and end date is 2016-02-20  Iwant to have remaining estimation is 40 .

 

IS there any way to do it using pot function ?groovy ?

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 17, 2016

Hi Rita,

Based on the requirements above I have attached a sample script below which will take the Values of the Start Date and End Date custom fields and calculate the number of days between them and times it by 10 to get the value to set the remaining estimate to.

When using the example above it gets the days between the 17-02-2016 and 20-02-2016 which is 4 and then multiplies by 10 get the value of 40 which it sets to the remaining estimate field.

Note if you wanted to change the value to a different value then you can change the calculation which defines the Long value that the setEstimate() method takes as a parameter.

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat;

// Set the logging leve to DEBUG
log.setLevel(org.apache.log4j.Level.DEBUG)

// Get the current issue key
Issue issueKey  = issue
def id=issueKey.getId()

// Get access to the Custom Field Manager
def customFieldManager = ComponentAccessor.getCustomFieldManager();

// Get the required custom field values
def StartDate = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Start Date'}
def EndDate = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'End Date'}
def startDateVal = StartDate.getValue(issue)
def endDateVal = EndDate.getValue(issue)

// Use Java simple date format to get the dates into a format we can work with in the script
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy")
// Format the date that we want to return
def formatStarDate = sdf.format(startDateVal)
def formatEndDate = sdf.format(endDateVal)

// Create date objects based on the formatted date values
Date startDateObject = new Date(formatStarDate);
Date endDateObject = new Date(formatEndDate);

// Make sure none of the date fields are null
if(startDateVal != null && endDateVal !=null){

    // Calculate the number of days between the Start Date and End Date
    def daysBetweenDates = endDateObject - startDateObject +1 // Add 1 as we want to include the current day

    // Set the Remaining estimate field
    def longEstimate = daysBetweenDates *10.toLong()
    issue.setEstimate(longEstimate)

}

// Save the issue
issue.store(); // save the issue

I hope this helps

Thanks

Kristian

Rita YOUNES [Infosysta]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 17, 2016

Dear @Kristian Walker (Adaptavist) thanks for you answer.

the code working but I'm not able to see the resulat in the "Original Estimation " field. 

Please help.

 

Thanks,

Rita

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 17, 2016

Hi Rita,

In your requirements you mentioned needing to set the remaining estimate which is a field in JIRA itself.

In order to set the original estimate field you should look to use the setOriginalEstimate() method.

I have attached a modified copy of the script below which shows how to set the Original Estimate as well as the remaining estimate.

import com.atlassian.jira.issue.*;
import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat;

// Set the logging leve to DEBUG
log.setLevel(org.apache.log4j.Level.DEBUG)

// Get the current issue key
Issue issueKey  = issue
def id=issueKey.getId()

// Get access to the Custom Field Manager
def customFieldManager = ComponentAccessor.getCustomFieldManager();

// Get the required custom field values
def StartDate = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Start Date'}
def EndDate = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'End Date'}
def startDateVal = StartDate.getValue(issue)
def endDateVal = EndDate.getValue(issue)

// Use Java simple date format to get the dates into a format we can work with in the script
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy")
// Format the date that we want to return
def formatStarDate = sdf.format(startDateVal)
def formatEndDate = sdf.format(endDateVal)

// Create date objects based on the formatted date values
Date startDateObject = new Date(formatStarDate);
Date endDateObject = new Date(formatEndDate);

// Make sure none of the date fields are null
if(startDateVal != null && endDateVal !=null){
    // Calculate the number of days between the Start Date and End Date
    def daysBetweenDates = endDateObject - startDateObject +1 // Add 1 as we want to include the current day

    // Set the Remaining estimate field
    def longEstimate = daysBetweenDates *10.toLong()
    // Sets remaining estimate
    issue.setEstimate(longEstimate)
    // Sets original estimate
    issue.setOriginalEstimate(longEstimate)
        
}

// Save the issue
issue.store();

 

Thanks

Kristian

Rita YOUNES [Infosysta]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 17, 2016

Thanks so much @Kristian Walker (Adaptavist).

I have another problem when the end date is the "Due Date" and due date is a field in the system how can I get it ?

Thank you so much

5 votes
Rita YOUNES [Infosysta]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 17, 2016

@Kristian Walker (Adaptavist)

 

I have added the following code 

import com.atlassian.jira.issue.MutableIssue

MutableIssue mutableIssue = (MutableIssue) issue;
def endDateVal=mutableIssue.getDueDate()

 

And now it work.

 

Thanks so much 

Rita YOUNES [Infosysta]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 25, 2016

Hi @Kristian Walker (Adaptavist)

 

How can we display the dates if we want them to appear in a transition ?

For example when I try to put start date and due date on transition they appear empty.

How can we get the value already exist in the issue.

 

Thanks

 

 

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 26, 2016

Hi Rita,

Can I ask what you are trying to do. Are you trying to reference the field in another script in a during the same transition that you have set them in as this will not work as the values will not have been updated yet until the issue created is fired and the issue is indexed. 

If you want the fields need to be displayed on a transition screen then they the fields need to be set before the transition is execured. IE it is not possible to set the fields and show them on a transition screen in the same transition.

I hope this helps.

Thanks

Kristian

TAGS
AUG Leaders

Atlassian Community Events