[ScriptRunner] update a sub-task(s) field 'x' value if parent ticket field 'x' value updated

Sam
Contributor
June 15, 2018

Hi,

I would like to write a groovy script for that but I'm new to groovy and I'm not sure where to start.

I'm thinking about an auto-update or popup to decide which fields you would like to update.

 

Thanks for any suggestions

4 answers

2 votes
Aidan Derossett [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.
June 19, 2018

Hey there @Sam!

Mark's answer is essentially correct, but could fail depending on the type of field that you're working with. Some fields can just be set with a String, but others need a list of Option objects and other sorts of things.

What sort of field are wanting to copy over? For instance, is it a regular system field (one of the built-ins), or a custom field? If it's a system field, which? And if it's a custom field, what type? This information will help in getting you the most accurate solution possible. 

Thanks! :D
Aidan

Daniel Rocha October 5, 2018

@Aidan Derossett [Adaptavist]. you are correct. I am trying to set the value based on a "Version Picker (multiple version)" and I am having troubles to work with it. Is there any tutorial to help me?

I basically need to copy the value of a custom field (Version Picker (multiple version)) from my parent issue to it's subtasks.

Sai Krishna Yadav Challendula September 1, 2021

Hi @Aidan Derossett [Adaptavist]

I'm looking for same script for Assignee system field need to update when i update in parent to subtask. Please help me out with below script.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Assignee"}
if (change) {
log.debug "Value changed from ${change.oldstring} to ${change.newstring}"
// your actions if the field has changed
def subtasks = event.issue.getSubTaskObjects()
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Assignee")
if (subtasks){
subtasks.each {it ->
((MutableIssue) it).setCustomFieldValue(customField, change.newstring)
ComponentAccessor.getIssueManager().updateIssue(event.user, ((MutableIssue) it), EventDispatchOption.DO_NOT_DISPATCH, false)
}
}

Thanks,

Sai

2 votes
Mark Markov
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.
June 15, 2018

Hello @Sam

You can create Script listener, that will be triggers when your X field changes and update subtasks issues if they exist.

To create listener go Add-ons -> Script Listener ->Add -> Custom listener

Choose project you need, event Issue Updated (that means that listener will be triggered every time issues in you project updates)

And add script, for example:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "your X field name"}
if (change) {
log.debug "Value changed from ${change.oldstring} to ${change.newstring}"
// your actions if the field has changed
def subtasks = event.issue.getSubTaskObjects()
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("your X field name")
if (subtasks){
subtasks.each {it ->
((MutableIssue) it).setCustomFieldValue(customField, change.newstring)
ComponentAccessor.getIssueManager().updateIssue(event.user, ((MutableIssue) it), EventDispatchOption.DO_NOT_DISPATCH, false)
}
}
}

 do not forget to correct code with your fieldnames.

Gavin Minnis July 30, 2018

@Mark Markov

Hi Mark, I'm also trying to achieve this technique. However, instead of a string, the fields I am trying to update are custom field Radio Buttons. Here is the code I have so far, which is not working:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Claim Approved"}
if (change) {
log.debug "Value changed from ${change.oldstring} to ${change.newstring}"
// your actions if the field has changed
def subtasks = event.issue.getSubTaskObjects()
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Claim Approved")
if (subtasks){
subtasks.each {it ->
((MutableIssue) it).setCustomFieldValue(customField, change.newstring)
ComponentAccessor.getIssueManager().updateIssue(event.user, ((MutableIssue) it), EventDispatchOption.DO_NOT_DISPATCH, false)
}
}
}

 How would I modify this for a Radio Button?

And if you have time, how would you modify this for a select list with various options? (This one isn't critical for me at the moment, but I'm sure I'll need it down the road)

 

Thanks

Mark Markov
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.
July 31, 2018

Hello @Gavin Minnis

If you want to update selectlists, radio buttons, checkboxes fields you need to pass Option object, not string.

Here is example.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Claim Approved"}
if (change) {
log.debug "Value changed from ${change.oldstring} to ${change.newstring}"
// your actions if the field has changed
def subtasks = event.issue.getSubTaskObjects()
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Claim Approved")

if (subtasks){
subtasks.each {it ->
def options = ComponentAccessor.getOptionsManager().getOptions(customField.getRelevantConfig(it))
((MutableIssue) it).setCustomFieldValue(customField, options.find {it.value == change.newstring})
ComponentAccessor.getIssueManager().updateIssue(event.user, ((MutableIssue) it), EventDispatchOption.DO_NOT_DISPATCH, false)
}
}
}
Like Sam likes this
Gavin Minnis July 31, 2018

Once again, you've solved my problem! Thanks, Mark.

Mark Markov
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.
July 31, 2018

You re welcome! :)

Daniel Rocha October 5, 2018

@Mark Markov, and if its a Version Picker (multiple version) custom field? Is it possible to update the sub-tasks if a specific field from the parent issue is changed?

My situation is:

1 - Parent issue have a "x" custom field (Version Picker (multiple version))

2 - If that "x" custom field is changed I want to update the same field on it's sub-tasks.

 

Thanks in advance.

Judah
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.
October 23, 2018

@higetora

 

My field is a Select List - Single Choice.  I have added your code but it is throwing a null value for my listener? 

 

Any advice would be greatly appreciated! 

0 votes
Deleted user November 21, 2018

@Mark Markov For some reason I can't get this script to work. I want to Fix Version/s -field to be updated and Script Listener shows "No failures in the last 4 execution(s)" but field in subtask is still out dated. 

0 votes
Sam
Contributor
June 20, 2018

Hi, 

Thank you for your answers. I've just started playing around with this script.
I would like to update: Custom fields - string, Description field - string, Dates fields.

Harish Kumar May 22, 2020

Hi @Sam @Mark Markov < how do i update summary value in subtasks based on parent issue custom field automatically.

Like ashleytse likes this
ashleytse January 28, 2022

how do i update summary value in subtasks based on parent issue custom field automatically. <-- i also wanna know same answer ;)

Suggest an answer

Log in or Sign up to answer