Using scriptrunner listener to copy contents of a parent field to subtask work field

Can anyone help me with Scriptrunner. I need to be able to create a scriptrunner listener that will copy a parents status (e.g. backlog, in progress etc..) to a subtask work field called "Parent Status". I also need to be able to copy a parent work field "RCA Date" to subtask custom field "Parent RCA Date". I need a listener so these subtask fields are updated whenever the parent field is updated. Thanks in advance.

1 answer

1 accepted

2 votes
Jenna Davis Community Champion Aug 23, 2017

Hello,

You should be able to do something similar to this for the parent status field:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

if(!event.issue.isSubTask()){
return
}

def customFieldManager = ComponentAccessor.customFieldManager

def parent = event.issue.getParentObject()
def status = parent.status.name

def parentStatus = customFieldManager.getCustomFieldObjectByName("Parent Status")
def changeHolder = new DefaultIssueChangeHolder()

parentStatus.updateValue(null, event.issue, new ModifiedValue(parentStatus.getValue(event.issue), status), changeHolder)

For the RCA Date field, it should be similar code. You'll just need to change what fields you're getting and updating. 

Note: I didn't test the code out directly, but I think it should work for you. 

If you have any trouble getting this to work or need help coding the script for your RCA field, let me know! I'll be glad to help you out further. :)

Jenna

Thank you Jenna. This is great.

However, this code only works when the sub-task is edited, taking the status from the parent issue and updating the sub-task 'parent status' field.

What I really need is for the sub-task 'parent status' field to be updated when the parent issue is edited or transitioned rather than when the sub-task is edited or transitioned.

Again, thanks in advance. Really appreciated.

Neil

Jenna Davis Community Champion Aug 25, 2017

You are completely correct, I overlooked that. I think you should be able to do this with similar code by using something like 

event.issue.getSubTaskObjects()

in order to access your subtasks, then loop through the collection of subtasks you get back and update them (similar to above). If you need help writing out code for this, let me know. 

Thanks Jenna. I'm not a developer so unsure how to structure your suggestion. If you do have time to write the code that would be awesome.

Neil

Here is a listener that should catch 'Issue Created' and 'Issue Updated' events. I also set my listener to catch 'Generic Events' and created post functions to fire generic events on transitions to ensure they are caught. You could also make this a custom event with some additions to the code. There are other community questions that cover how to catch custom events like this if want to do that and need help writing it. :)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

if(!event.issue.isSubTask() && !event.issue.getSubTaskObjects()){
return
}

def customFieldManager = ComponentAccessor.customFieldManager

if(event.issue.isSubTask()) {

def parent = event.issue.getParentObject()
def status = parent.status.name

def parentStatus = customFieldManager.getCustomFieldObjectByName("Parent Status")
def changeHolder = new DefaultIssueChangeHolder()

parentStatus.updateValue(null, event.issue, new ModifiedValue(parentStatus.getValue(event.issue), status), changeHolder)
}

if(event.issue.getSubTaskObjects()){
def subtasks = event.issue.getSubTaskObjects()
def status = event.issue.status.name
subtasks.each {
def parentStatus = customFieldManager.getCustomFieldObjectByName("Parent Status")
def changeHolder = new DefaultIssueChangeHolder()

parentStatus.updateValue(null, it, new ModifiedValue(parentStatus.getValue(it), status), changeHolder)
}

}

 

Please let me know if you have any more questions!

 

Jenna Davis Community Champion Sep 08, 2017

Did this code work for you?

This worked perfectly. Thank you so much for your help. You're a ⭐️

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

115 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you