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?

2 answers

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. 

Hi guys,

 

Does anyone know if the Original Estimate or Time Tracking fields can be updated with script runner?

I have searched a lot but I can't find an answer.

 

Regards,

Katerina

Thanos, 

What if my field is a Select List? How does that change the code you've provided? 

Hi @Thanos Batagiannis [Adaptavist],

I am trying to use your groovy script for my listener
Could you help me with this error please ? 
GroovyScriptListenerUpdateFields.png
NB : I changed issue to event.issue because I had this error : "[Static type checking] - The variable [issue] is undeclared". However, the other error was already there.

Thank you in advance,

Camille

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 16, 2018 in Jira

Looking for anyone who made the switch to Data Center

The Jira Marketing team is putting together an ebook on migrating to Data Center. We're looking for pro tips on how you staffed your project team and organized your Proof of Concept. Share yo...

63 views 0 2
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