Forums

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

Scriptrunner listener to update custom date field based on tasks linked to epic issue

Paul Kelly August 10, 2021

Hi All,

I need to write a listener that will update a custom date field (which would reside on an issue type of "epic") with the most distant future date contained in the "End Date" field of any tasks that may be linked to that epic.

 

Can @Ricehead Batagiannis [Adaptavist] , @Jamie Echlin [Adaptavist] or some of the other Adaptavist developers please help me out here?

Any help is much appreciated!

 

1 answer

0 votes
Ravi Sagar _Sparxsys_
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.
August 11, 2021

Hi @Paul Kelly 

Let me give you some pointers to solve this problem.

You can get the linked issues of an issue using the IssueLinkManager

 def links = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId())
  • In the snippet above pass the issue object (your epic).
  • Use either inward links or outward links in your code because links have direction.

Using the links variable you can then fetch the destination (outward link) or source (inward link).

links.each { it ->
def destinationObject = it.getDestinationObject()
//get the value from the end date here

endDates.push(endDateFromTheIssue)
}

You can create a list to store all the end dates.

def endDates = []

Sort the list and get the last date.

endDates.sort().last() 

  Use this date to write back to the custom field to store the latest end date.

These links will help you with some sample code.

I hope it helps. Try if you can write the code and if you have any issues then share your code, we will try to make it work for you.

Ravi

Paul Kelly August 11, 2021

Thanks for reaching out, @Ravi Sagar _Sparxsys_ - Unfortunately, none of the code you provided runs.

Can @Thanos Batagiannis [Adaptavist] , @Jamie Echlin [Adaptavist] or some of the other Adaptavist developers please help me out here?

 

Do you think I would be better to approach this in a slightly different way?  Such as to:

1. have a listener on the field "End Date" of each issue (not epics)

2. When "End Date" is changed then update the custom field value "Projected End Date" on any linked epic issues if the value of "End Date" is a more distant future date than what is already populated in "Projected End Date"?

 

I have used your code suggestions along with some code from the links you provided to attempt to create the listener logic described above.  Note that the end is in pseudo code because I am unsure as yet how best to approach it (again, thank you greatly for all of your assistance with this):

 

 


import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.IssueFieldConstants


// the name of the field to copy
final String fieldNameToCopy = "End Date"
// list of End Dates
def endDates = []

def fieldManager = ComponentAccessor.fieldManager

def fieldToCopy = fieldManager.allAvailableNavigableFields.find { it.name == fieldNameToCopy }
if (!fieldToCopy) {
log.info "Could not find field with name $fieldNameToCopy"
return
}

// attempting to get any links to an epic
// QUESTION - How to get the id of the issue in which this listener resides instead of "issue.getId()"?
def links = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId())
if (!links) {
log.info "There are no linked issues"
return
}

// Using the links variable you can then fetch the destination (outward link)
links.each { it ->
def destinationObject = it.getDestinationObject()
//get the value from the end date here
endDates.push(endDateFromTheIssue)
}

// get end date with most distant future date value
def lastEndDate = endDates.sort().last()


// PSEUDO CODE of the rest
//
// Update to value of a custom field "Projected End Date" which resides on the linked issue with the value from lastEndDate

// Sytax perhaps similar to that found on library link provided ( https://library.adaptavist.com/entity/update-the-value-of-a-custom-field-using-a-listener )

// customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), newValue), new DefaultIssueChangeHolder())

Suggest an answer

Log in or Sign up to answer