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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

365 views 0 6
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