It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Script Runner- Update subtask custom field based on parent

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

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

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

@Rafael Moreira Hi
Use it:

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

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)
}
}

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira

The add-in you’ve been waiting for: Jira Cloud for Excel 🙌

Introducing Jira Cloud for Excel Here at the product integrations team at Atlassian, we are thrilled to announce the new Jira Cloud for Excel add-in! This add-in lets you export Jira data directly ...

888 views 7 24
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you