Using Scriptrunner to copy a field value from Epic field to Requirement/User Story and Sub-Tasks

Sasha Sun September 1, 2021

Hi all,

We have a custom dropdown field called "Project Group" for Epic, Requirement/User Story and its sub-tasks. Now, we have to manually this field value. 

I would like to achieve "Project Group" of Requirement/User Story could inherit the field value from Epic directly, also sub-tasks could inherit the field value from Requirement/User Story. 

I did found some similar posts and answers on community, but they do not work for my case.  As Requirement/User Story is actually not a real child tickets of Epic, it's the issue in Epic, please see below. Sub-tasks could be children tickets of Requirement/User Story or Epic. Epic and Requirement-User Story.png

Can anyone please help me writing up the script. Thanks in advance!

 

 

 

 

3 answers

1 accepted

2 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 6, 2021

Hi @Sasha Sun

For your requirement, you could try something like this:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue

def issue = event.issue as MutableIssue

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def issueLinkManager = ComponentAccessor.issueLinkManager
def projectGroup = customFieldManager.getCustomFieldObjectsByName('Project Group').first()
def epicLink = customFieldManager.getCustomFieldObjectsByName('Epic Link').first()
def projectGroupValue = issue.getCustomFieldValue(projectGroup)

def subTasks = [] as ArrayList<Issue>

if (issue.issueType.name == 'Epic' && issue) {
def links = issueLinkManager.getOutwardLinks(issue.id)
links.each {
def destinationIssue = it.destinationObject as MutableIssue
destinationIssue.setCustomFieldValue(projectGroup, projectGroupValue)
issueManager.updateIssue(loggedInUser, destinationIssue, EventDispatchOption.DO_NOT_DISPATCH,false)

destinationIssue.subTaskObjects.findAll {
subTasks.addAll(it)
}
}
} else if(issue.getCustomFieldValue(epicLink)) {
def links = issueLinkManager.getInwardLinks(issue.id)
links.each {
issue.setCustomFieldValue(projectGroup, it.sourceObject.getCustomFieldValue(projectGroup) )
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH,false)
}
} else if(issue.isSubTask()) {
subTasks.addAll(issue)
}

subTasks.each {
def subTask = it as MutableIssue
subTask.setCustomFieldValue(projectGroup, it.parentObject.getCustomFieldValue(projectGroup))
issueManager.updateIssue(loggedInUser, subTask, EventDispatchOption.DO_NOT_DISPATCH,false)
}

Please note, this working sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a print screen of the listener configuration:-

listener_config.png

 

What this code does is that if the Project Group field is updated in an Epic, all the issues in the epic, including the sub-tasks, will update the value according to the value set in the Epic.

Also, suppose any new issue or sub-task is created and is associated with the epic. In that case, the Project Group value in the new issue will automatically be set according to the value in the Epic.

 

I hope this helps to answer your question. :)

 

Thank you and Kind Regards,

Ram

Sasha Sun September 8, 2021

This is perfect!! Thank you so much!!!

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 8, 2021

Hi @Sasha Sun

Great to hear the solution worked for you. :)

Kindly accept the solution provided.

 

Thank you and Kind Regards,

Ram

dorin gez October 5, 2021

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

Does this listener handles the case of removing Story from it's Epic?

The wanted result is null for the custom field.

 

Thank you,

Dorin

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 9, 2021

Hi @dorin gez

To answer your question, the listener example provided above does not do what you are expecting.

It only updates a particular custom field value in all the issues and sub-tasks of an epic if that specific custom field's value is updated in the parent epic itself.

What you are expecting can be done but will require a different code and listener configuration.

Thank you and Kind Regards,

Ram

Olivier Croquette December 16, 2022

The code works perfectly for us, thanks for sharing.

0 votes
Jeff JEAN October 15, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_

That script is useful, thanks 

However, I've tried to build on and add a level above the Epic based on Advanced Roadmaps "Parent Link" 

The hierarchy is : Feature > Epic > Standard issue > Sub-task 

The Epic is setp/ updated correctly but not the standard issue and the Sub-task levels 

Can you please explain how to include 1 or more levels above the Epic based on Advanced Roadmaps "Parent Link" ? 

Thanks 

0 votes
Jeff JEAN October 15, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_

That script is useful, thanks 

However, I've tried to build on and add a level above the Epic based on Advanced Roadmaps "Parent Link" 

The hierarchy is : Feature > Epic > Standard issue > Sub-task 

The Epic is setp/ updated correctly but not the standard issue and the Sub-task levels 

Can you please explain how to include 1 or more levels above the Epic based on Advanced Roadmaps "Parent Link" ? 

Thanks 

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 15, 2023

Hi @Jeff JEAN

It would be best if you create a separate question for this and mention my name in it as your requirement requires a lot of modifications.

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events