Hi,
I'm trying to find the best way to find the min and max dates of two custom fields of a group of stories related to an epic. We have two custom fields that we would like to get the earliest date and the latest date of and have the scripted field update in the epic it is related to. Any references/thoughts/suggestions of a ScriptRunner flow that would assist with that task?
Thanks in advance!
Hey James!
So, to clarify, are you saying that you want to pull two custom field values from an epic (which both consist of a list of stories related to that epic), and then, for each custom field, get the stories with the minimum and maximum dates? Then, once you find those, you want to output those stories to a script field? Additionally, what are the dates that you're referring to? Resolution dates, creation dates, etc...?
I can give you a better answer after you confirm the above, but conceptually, in a script field you could grab the values from those custom fields (assuming they are issue keys or something), get their associated issues, loop through the issues, and then do some sort of comparison using the java built-ins for Date objects: Date.before(Date), Date.after(Date), etc... While looping through, you could store the issues with the lowest and highest dates in variables to be returned later.
Again, I could probably code something up as an example after you answer those questions! :D
Thanks!
Aidan
Hi Aidan,
thank you you for the quick reply! I would like to roll up max and min dates of target dates of the stories that are associated to an Epic. So I would like to get the stories of an epic and then find all of the results of a custom date field to find the max or min to update a scripted field in the Epic. Let me know if that clears things up. Thank you again!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No problem! :)
So you're getting all of the stories of an epic, and for each of those stories you're getting the values from one of their custom date fields. From those values you want to find the max and min. Then you want to use that collection of maximums and minimums in some way to update the script field of the epic. Correct?
In what way do you plan on using those maximum and minimum dates to update the epic's field?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
OOOOH or are you saying that each of the stories has a custom date field with a single value, and you want to collect all of those values from every story and find the max and min out of those?
Sorry I'm questioning so much. I'm just trying to wrap my head around the scenario haha.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey again James!
I think I more or less understand what you're looking for and have written up a quick example of how you may do this in a Script Field. However, keep in mind that this example is untested:
import com.atlassian.jira.component.ComponentAccessor
//Check that current issue is an Epic
if(issue.issueType.name == "Epic")
{
//Initialize appropriate managers
def linkManager = ComponentAccessor.issueLinkManager
def cfm = ComponentAccessor.customFieldManager
//Get the story's date custom field and initialize a list to hold the date values
def storyDateField = cfm.getCustomFieldObjectByName("Story Date Field")
def storyDates = [] as List<Date>
//Loop through all of Epic's stories and collect their date objects
linkManager.getOutwardLinks(issue.id).each{
if(it.issueLinkType.name == "Epic-Story Link")
{
def storyIssue = it.destinationObject
storyDates.add(storyIssue.getCustomFieldValue(storyDateField) as Date)
}
}
//Initialize min and max
def minDate = storyDates[0] as Date
def maxDate = storyDates[0] as Date
//Find actual min and max Dates
for(int i = 1; i < storyDates.size(); i++)
{
def currentDate = storyDates[i]
if(currentDate.before(minDate))
{
minDate = currentDate
}
if(currentDate.after(maxDate))
{
maxDate = currentDate
}
}
//You now have the overall maximum and minimum of all of the Story's custom Date fields
//From here on out you can do whatever you need with those minimums and maximums
}
else //Return null if issue is not an Epic
{
return null
}
See if you can get that working for you and let me know if I'm still misunderstanding the goal! :D
Best,
Aidan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you Aidan, this worked exactly as wanted without any modifications at all! Thank you so much, truly appreciate the quick response and solution!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Just one quick follow up question Aidan, can you tell me how to format the min/maxDate to store it in a date/time picker, it is giving me an 'Invalid Date' but works fine if i'm using a text field. Thanks in advance!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No problem at all! :D
When exactly are you getting the 'Invalid Date' message?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm getting an invalid date when I try to store it as a date/time picker field rather than a text field, I'm not sure how to convert it to a date type for the date/time field to accept it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Could you show me how you're doing this in your code? :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sure, I have it exactly as your example:
import com.atlassian.jira.component.ComponentAccessor
//Check that current issue is an Epic
if(issue.issueType.name == "Epic")
{
//Initialize appropriate managers
def linkManager = ComponentAccessor.issueLinkManager
def cfm = ComponentAccessor.customFieldManager
//Get the story's date custom field and initialize a list to hold the date values
def storyDateField = cfm.getCustomFieldObjectByName("End date")
def storyDates = [] as List<Date>
//Loop through all of Epic's stories and collect their date objects
linkManager.getOutwardLinks(issue.id).each{
if(it.issueLinkType.name == "Epic-Story Link")
{
def storyIssue = it.destinationObject
storyDates.add(storyIssue.getCustomFieldValue(storyDateField) as Date)
}
}
//Initialize min and max
def minDate = storyDates[0] as Date
def maxDate = storyDates[0] as Date
//Find actual min and max Dates
for(int i = 1; i < storyDates.size(); i++)
{
def currentDate = storyDates[i]
if(currentDate.before(minDate))
{
minDate = currentDate
}
if(currentDate.after(maxDate))
{
maxDate = currentDate
}
}
//You now have the overall maximum and minimum of all of the Story's custom Date fields
//From here on out you can do whatever you need with those minimums and maximums
return(minDate)
}
else //Return null if issue is not an Epic
{
return null
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Aidan Derossett [Adaptavist] Hi Aidan, I have a similar query , can you please help.
My scenario is, I want to have a scripted field in PROGRAM EPIC. So what this field should basically do is, it must automatically pick the most late date based off the "DUE DATE" field in the EPIC.
Example: If
ABC EPIC-1 DUE DATE is 08/10/18
ABC EPIC-2 DUE DATE is 10/10/18
ABC EPIC-3 DUE DATE is 12/10/18
ABC PROGRAM EPIC scripted DATE field should automatically pick 12/10/18
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.