Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,555,711
Community Members
 
Community Events
184
Community Groups

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

Edited

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!

 

 

 

 

1 answer

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.
Sep 06, 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

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.
Sep 08, 2021

Hi @Sasha Sun

Great to hear the solution worked for you. :)

Kindly accept the solution provided.

 

Thank you and Kind Regards,

Ram

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.
Oct 09, 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
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
Dec 16, 2022

The code works perfectly for us, thanks for sharing.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events