Update Custom Field Value using a “ScriptRunner for Jira” Custom Listener

I have a list of custom fields in sub-tasks that need to be update automatically whenever the associated field is update in the parent.  The fields are the same from parent to sub-task

PO Number (Parent) to PO Number (Sub-task)

Tracking Number (Parent) to Tracking Number (Sub-Task)

Vendor Name (Parent) to Vendor Name (Sub-Task)

Vendor Contact Name (Parent) to Vendor Contact Name (Sub-Task)

Vendor Email (Parent) to Vendor Email (Sub-Task)

Any time any one of those fields are updated in the parent i need it to be updated across all of the sub-tasks. 

Can someone help me write this listener?

1 answer

1 accepted

Hi Scott, 

What it really matters when trying to update a custom field is the type of it. From your description I can understand that the fields you want to update are going to be either a TextField or a Number Field. 

So as you said you will need a listener listening for an issue updated event. 

The script for this listener should be similar to 

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

// Check it the update that just happened is because of "TextFieldA" or "Number CF"
def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field in ["TextFieldA", "Number CF"]}

//the change is not in one of the fields we are interested in or the issue has not subtasks - do nothing
if (!change || !issue.subTaskObjects) {
return
}

issue.subTaskObjects?.each {
syncCustomFieldValue(issue, it, change.field)
}

/**
* Sync the value (update childIssue value according to parent's issue one)
* @param parentIssue
* @param childIssue
* @param fieldName
*/
def syncCustomFieldValue(MutableIssue parentIssue, MutableIssue childIssue, String fieldName) {
def changeHolder = new DefaultIssueChangeHolder()
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(fieldName)
def newValue = parentIssue.getCustomFieldValue(cf)
cf.updateValue(null, childIssue, new ModifiedValue(childIssue.getCustomFieldValue(cf), newValue), changeHolder)
log.debug "Field $fieldName updated"

Hope that helps, 

Thanos

Thanos!!!!

That worked great! Thank you so much. How do i get it so it updates across all sub-tasks and not just the first?

Hey Scott, 

That's great. I updated the script above to make the update to all the subtasks. 

Regards, Thanos

Wow that was it?!?!  Crazy. It works perfectly. Thank you. 

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,110 views 13 19
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot