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

How to set a scripted field to get a date from another field ?

Aisha M Apr 05, 2019

I need to set up a scripted date field that picks a date from the Due date field.

The scripted date field must pick the most furthest date from that field.

Can someone please help with a script or a similar script that matches this scenario. Thank you 

2 answers

1 accepted

1 vote
Answer accepted
Antoine Berry Apr 05, 2019

Hi,

What do you mean by "The scripted date field must pick the most furthest date from that field." ?

Aisha M Apr 08, 2019

@Antoine Berry  Thank you for the reply.

So, We want to have a scripted date field in "PROGRAM EPIC" issue type

This scripted field must display the farthest date value from the DUE DATE field available in EPIC (related to the Program Epic)

Example:

EPIC 1 (due date) - 4/9/19

EPIC 2 (due date) - 3/9/19

EPIC 2 (due date) - 1/9/19

PROGRAM EPIC (scripted date field) - 4/9/19

Antoine Berry Apr 08, 2019

Hi,

I am sorry my brain might be a bit slow on monday morning. Could you provide a screenshot ?

I guess you have a custom issue type (program epic), and you are linking epics to it ? And you want to retrieve the farthest date from all these linked epics ?

Aisha M Apr 08, 2019

@Antoine Berry  Lol :) Unfortunately we have the screentshot options disabled for security reasons.

But your understanding is absolutely correct ! 

Aisha M yesterday

@Antoine Berry Sorry for troubling you. But, can I please some some help on this. I tried using the script mentioned in the below comment, but did not work

Antoine Berry 6 hours ago

Hi @Aisha M ,

I have had a hard time solving this, as I did not have much experience with scripted fields, but I am glad I succeeded ! 

Here is the script I used : 

import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.component.ComponentAccessor;
import java.sql.Timestamp

def allOutIssueLink = new ArrayList<IssueLink>(ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId()))
def issueLinks = allOutIssueLink?.findAll() {it.getDestinationObject().getIssueType().getName() == "Epic"}
def farthestDate


for(IssueLink issueLink : issueLinks){
def dueDate = issueLink.getDestinationObject().getDueDate()

if (farthestDate == null){
farthestDate = dueDate
}
else if (dueDate?.after(farthestDate)){
farthestDate = dueDate
}
}

def allInIssueLink = new ArrayList<IssueLink>(ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId()));
issueLinks = allInIssueLink?.findAll() {it.getSourceObject().getIssueType().getName() == "Epic"}

for(IssueLink issueLink : issueLinks){
def dueDate = issueLink.getSourceObject().getDueDate()

if (farthestDate == null){
farthestDate = dueDate
}
else if (dueDate?.after(farthestDate)){
farthestDate = dueDate
}
}

return farthestDate

It checks against the due dates of all linked issues of type "Epic" (make sure this is the exact issue type name).

Screenshot of the configuration : 

image.png

Result : 

image.png

Let me know if that works for you !

Antoine

Aisha M 4 hours ago

@Antoine Berry  Thank you soo much for taking your time on this ! :)

So, this script picks the fathest due date out of all the epics and displays at the Program Epic (higher hierarchy than epic)  level ?

Antoine Berry 4 hours ago

Yes, you need to configure the field to be displayed on Program Epic issue type only :

image.png

Aisha M 4 hours ago

@Antoine Berry  Omg ! It worked !! Thank you so much . .  you are a genius ! ! :) I ll trt testing it out a couple more times and see if I face any backend issues ! Wow yer the best !!!

Antoine Berry 4 hours ago

Ahah, I'm glad it worked out. :)

If you have any problem feel free to come back to me !

Like Aisha M likes this
0 votes
Thomas Ivall Apr 08, 2019
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def dueDateField = customFieldManager.getCustomFieldObjectByName("Due Date")

List<IssueLink> linkedIssues = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId())

if(linkedIssues.size() < 1){
return
}

def farthestDate = new Timestamp(Long.MIN_VALUE)

for(IssueLink iLink : linkedIssues){
def iLinkIssue = issueManager.getIssueObject(iLink.getId())

log.error(iLinkIssue.getId())
Timestamp dueDate = (Timestamp) iLinkIssue.getCustomFieldValue(dueDateField)

log.error(dueDate)
if(dueDate.after(farthestDate)){
farthestDate = dueDate
}
}

return farthestDate

You will need to configure your Farthest Date to be a Date Time range picker in the custom field settings page.

Aisha M Apr 08, 2019

@Thomas Ivall  

Thank you so much for the comment !

So, will the script display the farthest day of the Epics linked at the Program Epic (issue type higher than epic) level ? 

Thomas Ivall Apr 09, 2019

@Aisha M 

No problem! Yes it will get the inward links of the Program Epic (as long as you put it on the screen for that issue type) which will be various Epics. From there it'll loop over the linked issues (there is no checking that they're of a specific issue type, you can add this in if there will be other issue types linked to this issue) and find the farthest date out of all their due dates.

Let me know if there are any problems!

Aisha M Apr 09, 2019

@Thomas Ivall Thank you so so much for helping out ! :)

I did follow all of the steps. But my  Program Epic is not showing the farthest date from the due date values in epic. dunno where I m going wrong 

Thomas Ivall Apr 09, 2019

@Aisha M  If you go to the Scripted Fields part of ScriptRunner in the Add-ons settings there will be outputs to the logs, could you attach a picture of the most recent log?

Also on the Program Epic issue can you in the Admin section select the Where is my field? button and search for the scripted fields name and attach that.

Aisha M Apr 09, 2019

@Thomas Ivall Well, do to compliance issues, we do not have sceenshots emabled. But, I copied the log output,

2019-04-08 07:20:34,878 ERROR [customfield.GroovyCustomField]: *************************************************************************************
Script field failed on issue: ATLAS-570, field: Farthest Date
java.lang.NullPointerException: Cannot invoke method getId() on null object
 at Script6.run(Script6.groovy:21)
Thomas Ivall Apr 09, 2019
if(iLink == null){
continue;
}

can you add this bit of code just beneath the for(IssueLink iLink : linkedIssues){ part of the code. 

Aisha M Apr 11, 2019

@Thomas Ivall The script seems okay with no errors (at the inline script screen). But I m still not able to see the farthest date field value on the PROGRAM EPIC issue. I have added the scripted field to the relevant screens & everything . . But still doesn't show up. Don't know where I'm going wrong. 

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Jira

Try Jira Cloud for Outlook: Organize your work without leaving your inbox

Hi Atlassian community, My name is Max and I work on the product integration team at Atlassian. I am pleased to announce the early access program for the Jira Cloud add-in for Outlook. This add-in...

1,629 views 3 15
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