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?
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey Scott,
That's great. I updated the script above to make the update to all the subtasks.
Regards, Thanos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Wow that was it?!?! Crazy. It works perfectly. Thank you.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanos,
What if my field is a Select List? How does that change the code you've provided?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Thanos Batagiannis [Adaptavist],
I am trying to use your groovy script for my listener
Could you help me with this error please ?
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.