Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Custom field based on Priority + created date

Davor Fisher
Contributor
February 24, 2016

Hello all,

I am looking to create a custom field that will be based on create date plus priority. This field will be mostly used for reporting purposes on columns. I found few examples on creating a listener or adding a workflow create post function. Would it be possible just to go with a custom field, rather then creating something on a workflow.

Example would be:

Create date + 1d if it's a P1, +2d if it's a P2, and +3d if it's a P3.

 

Any suggestions would be greatly appreciated.

Thanks

2 answers

1 accepted

1 vote
Answer accepted
Kristian Walker _Adaptavist_
Community Champion
February 24, 2016

Hi DF,

I have attached some example code of how you can use to create a scripted field using a Text Field template that will take the created date and add a number of days to it based on the priority. This field returns a string value of the date which you may find easier to use for reporting purposes.

import com.atlassian.jira.issue.Issue
import org.apache.commons.lang.time.DateUtils;
import java.text.SimpleDateFormat;
import java.util.Date.*;

// Get the Jira issue key and its priority
Issue issue  = issue
def Priority =  issue.getPriorityObject().getName()
def tz = TimeZone.getTimeZone('GMT')
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-YYY");
Date created = issue.getCreated()
def strDate = sdf.format(created);

// Add values to the current date depending on the priority
if(Priority == "P1"){
    // Add 1 Day
    strDate = DateUtils.addDays(created,1).format("dd-MM-YYYY")
} else if(Priority == "P2"){
    // Add 2 Days
        strDate = DateUtils.addDays(created,2).format("dd-MM-YYYY")
}else if(Priority == "P3"){
    // Add 3 Days
        strDate = DateUtils.addDays(created,3).format("dd-MM-YYYY")
} else if(Priority == "P4"){
    // Add 4 Days
        strDate = DateUtils.addDays(created,4).format("dd-MM-YYYY")
}
else if(Priority == "P5"){
    // Add 5 Days
        strDate = DateUtils.addDays(created,5).format("dd-MM-YYYY")
}
return strDate

 

I hope this helps.

Thanks

Kristian

Davor Fisher
Contributor
February 25, 2016

Thanks, works great - Had to make it:

Searcher: Free Text Searcher

Template: Text Field (multi-line)

Kristian Walker _Adaptavist_
Community Champion
February 25, 2016

Hi  DF,

That is the correct searcher and I am glad I could help.

If this answer is useful can you please mark it as accepted so other people with a similar question can easily find this answer in future.

Thanks

Kristian

1 vote
Boris Georgiev [Appfire]
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 24, 2016

Create a scripted field with the following code:

import org.joda.time.DateTime;
if(issue.priorityObject.name.contains("P1")) {
return new DateTime(issue.created).plusDays(1).toDate()
}
if(issue.priorityObject.name.contains("P2")) {
return new DateTime(issue.created).plusDays(2).toDate()
}
if(issue.priorityObject.name.contains("P3")) {
return new DateTime(issue.created).plusDays(3).toDate()
}
issue.created

You should use:

Searcher: Date Time Range picker
Template: Date Time Picker

Davor Fisher
Contributor
February 25, 2016

Thanks, this works as well. One minor change i had to make was changing the template to text field, because using date time picker, wouldn't give me an actual date, but it would say: 2 days, in 4 hours, etc

 

Making it Text field (multi-line): i got this format

example: Mon Feb 29 18:26:35 EST 2016

Davor Fisher
Contributor
June 15, 2016

Hi,

 

We have upgraded to JIRA 7 and this custom field is throwing an error now. Do you know why?

See attachmentScreen Shot 2016-06-15 at 2.10.49 PM.png

Boris Georgiev [Appfire]
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.
June 15, 2016

I think the variable "Issue" should be "issue" with lower letter 'i'

Davor Fisher
Contributor
June 15, 2016

Hi,

 

Thanks for quick reply - we noticed that mistake and made corrections but now are asking to make more changes. Screen Shot 2016-06-15 at 3.20.54 PM.png

Davor Fisher
Contributor
June 15, 2016

This was resolved by: 

Switching 

issue.priorityObject.name.contains ("1-urgent")

to 

issue.getPriority ().name == ("1-Urgent")

Suggest an answer

Log in or Sign up to answer