Custom field based on Priority + created date

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

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

Thanks, works great - Had to make it:

Searcher: Free Text Searcher

Template: Text Field (multi-line)

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

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

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

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

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

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

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

747 views 1 18
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you