Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script Runner- Update subtask custom field based on parent

laurahbantug
Contributor
April 9, 2018

Hi,

Scenario: My team sends a Tempo extract of hours logged on tickets to finance team once a month.  Before we send the report- we need to ensure all the ticket fields are flagged properly.  A major issue we encounter is when subtasks custom fields become out of sync from their parent tasks.  I have the subtasks inheriting parent custom fields on create- however when someone updates a field on the parent and forgets to update the children they can become out of sync.

Question: Can I get help writing a script I can run on the Script Runner console once a month before I pull the report to ensure all the parent and child tickets in the project are in sync?  Is this feasible or is there a better solution?  Thanks so much

1 answer

1 accepted

1 vote
Answer accepted
Roland Holban (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.
April 12, 2018 edited

A better solution would be writing a script listener that fires on issueUpdated event and fills in the child custom fields accordingly. 

https://scriptrunner.adaptavist.com/latest/jira/listeners.html#_custom_listeners

Rafael Moreira October 22, 2018

Hi,

I have the same problem and I have  try a code but nothing happend

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 == "Labels"}
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("Labels")
    if (subtasks){
        subtasks.each {it ->
            ((MutableIssue) it).setCustomFieldValue(customField, change.newstring)
            ComponentAccessor.getIssueManager().updateIssue(event.user, ((MutableIssue) it), EventDispatchOption.DO_NOT_DISPATCH, false)
        }
    }
}

 Can you help me please ?

Thank you

Alexander
Contributor
July 17, 2019

@Rafael Moreira Hi
Use it:

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "labels"}

Charles Huggins October 24, 2019

Hi Folks,

This mine script (works for me)

Perhaps it helps to some1 else.

import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

def event = event as IssueEvent
def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Contributors"}

if (event) {
def issue = event.issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def changeHolder = new DefaultIssueChangeHolder()

def targetField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "Contributors"}
def sourceField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "Contributors"}

targetField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(targetField), issue.getCustomFieldValue(sourceField)), changeHolder)

//update the UserPicker2 in subtasks as well - with the value of UserPicker from parent issue
issue.subTaskObjects?.each { subtask ->
targetField.updateValue(null, subtask, new ModifiedValue(subtask.getCustomFieldValue(targetField), issue.getCustomFieldValue(sourceField)), changeHolder)
}
}
Like 2 people like this
Tanu
Contributor
January 13, 2020 edited

Hi Guys,

I have a requirement where i need help.

When a custom field is getting updated in subtask , then changes should automatically reflect in parent task.

Please help

Thanks in advance.

Like Teja likes this
Tanu
Contributor
June 18, 2020

I have automated this now

Like Joe Yesh likes this
Joe Yesh July 10, 2020

Hi Tanu,

Can you share your code snippet if your automation is working fine?

I too same requirement where I need few fields in the parent should sync to sub-tasks whenever there is a change in parent ticket.

 

thanks

Tanu
Contributor
July 10, 2020

@Joe Yesh I have ended up using a postfunction available from JWME add-on, on subtask workflow. 

Copy field value to parent issue (JMWE add-on)Copy the value(s) of a field to a field of the parent issue
Like 2 people like this
Inayat Nahvi
Contributor
May 12, 2021

The listener @Charles Huggins works perfectly for issues edits.   The post functions that @Tanu pointed work great upon issue creation.

Sai Krishna Yadav Challendula September 1, 2021

Hi @Charles Huggins ,

Can you pls help out with this script. I would like to update the Assignee field value Parent to Subtask. If i update Parent assignee field value it should update for all Subtasks. Please find my below script.

import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

def event = event as IssueEvent
def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Assignee"}
log.warn("Assignee+++")
if (event) {
def issue = event.issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def changeHolder = new DefaultIssueChangeHolder()

def targetField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "Assignee"}
def sourceField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "Assignee"}

targetField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(targetField), issue.getCustomFieldValue(sourceField)), changeHolder)
log.warn("Target++++")
//update the UserPicker2 in subtasks as well - with the value of UserPicker from parent issue
issue.subTaskObjects?.each { subtask ->
targetField.updateValue(null, subtask, new ModifiedValue(subtask.getCustomFieldValue(targetField), issue.getCustomFieldValue(sourceField)), changeHolder)
log.warn("Target,,,,,,")
}
}

I'm getting below error

error.PNG Thanks,

Sai

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Atlassian Community Events