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 ?

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 Community Leader Apr 05, 2019

Hi,

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

@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 Community Leader 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 ?

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

But your understanding is absolutely correct ! 

@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 Community Leader May 22, 2019

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

@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 Community Leader May 22, 2019

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

image.png

@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 Community Leader May 22, 2019

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

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

Like Aisha M likes this

@Antoine Berry Just a small request, Is it possible include in the script the below scenario,

If any of the Epic due dates are empty, then the Program epic due date must be empty too.

Thank you :)

Antoine Berry Community Leader Jul 01, 2019

Hi @Aisha M ,

Sorry, I was out of office. I modified the script above so that if any linked Epic has no due date, the program epic du date will be empty : 

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 (dueDate == null){
return null
}
else 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 (dueDate == null){
return null
}
else if (farthestDate == null){
farthestDate = dueDate
}
else if (dueDate?.after(farthestDate)){
farthestDate = dueDate
}
}

return farthestDate

Antoine

@Antoine Berry  Thank you ! Will test this out and let you know :) . .  So grateful for your help all the time  :):) Thanks again

Like Antoine Berry likes this

@Antoine Berry  Works PERFECT !!!! Thanks a million ! :)

Like Antoine Berry likes this
Antoine Berry Community Leader Jul 10, 2019

Always glad to help ! Glad it worked out. :)

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.

@t-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 ? 

@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!

@t-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 

@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.

@t-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)
if(iLink == null){
continue;
}

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

@t-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
Community showcase
Published in Jira

Managing Incidents - The roof is on FIRE!

The roof is on FIRE… network outages, broken processes, upset clients and employees. Each day seemed to bring more and more issues. Incidents were communicated via email, messengers (skype or teams) ...

119 views 0 1
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