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,362,968
Community Members
 
Community Events
168
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

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

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

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

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events