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

Scott Federman December 2, 2017

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?

3 answers

1 accepted

1 vote
Answer accepted
Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 4, 2017

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

Scott Federman December 4, 2017

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?

Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 5, 2017

Hey Scott, 

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

Regards, Thanos

Scott Federman December 5, 2017

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

tifonas-katrina March 28, 2018

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

Judah October 23, 2018

Thanos, 

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

0 votes
Mindaugas Kuodis September 5, 2019

Dear @Thanos Batagiannis _Adaptavist_ ,

is there a chance that you could share modified version for cascading filed update? I have one cascading filed on Parent issue and would like to reflect its values on child issues whenever parent filed values changes or if reconnect child to other parent.

I'm not sure if it could help, but I have figure out how to make scripted filed which can return cascading field first or secondary options:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

def issue = issue
CustomField cf = customFieldManager.getCustomFieldObject(14103L) //customfield id
Map cfVal = issue.getCustomFieldValue(cf) as Map

if (cfVal) {
String first = cfVal.get(null) // return value of 1st selected option in cascading field
//String second = cfVal.get('1') // return value of 2nd selected option in cascading field
}
else {
log.debug("Custom field not present on this issue")
}

0 votes
Camille Lecerf June 17, 2018

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

Kurkulik June 27, 2019

check input parameters

Add in the logs the values ​​of the variables that are passed in 13 string and follow the conditions for triggering the listener

log.warn ("issue" + event.issue)

log.warn ("it" + it)

log.warn ("change filed" + change.field)

 

most likely a mistake in them

Suggest an answer

Log in or Sign up to answer