Update a Single Select Field of a Parent when Subtask Transitions

Hello,

Has anyone ever scripted a post function that when a child subtask transitions, a single select field of the parent would get updated too?

Example:
Subtask transitions to Done, Parent field updated from "No" to "Yes".

The closest example I've found:

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.ComponentManager
import com.atlassian.crowd.embedded.api.User

if (issue.isSubTask()) {
MutableIssue parentIssue = issue.getParentObject();
parentIssue.setPriorityID("3")

User user = ... // Grab your user here

ComponentManager.getInstance().getIssueManager().updateIssue(user, parentIssue, EventDispatchOption.DO_NOT_DISPATCH, true)

1 answer

Hey,

So in your case you can have a Custom Script post function in your subtask's workflow, and the script will look like 

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

def issue = issue as MutableIssue

// the issue is not a subtask - do nothing
if (! issue.isSubTask()) {
return
}

def parentIssue = issue.parentObject
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("SelectListA")

// get the option with value AAA
def optionToSet = ComponentAccessor.optionsManager.getOptions(cf.getRelevantConfig(parentIssue)).find {it.value == "AAA"}

def changeHolder = new DefaultIssueChangeHolder()
cf.updateValue(null, parentIssue, new ModifiedValue(issue.getCustomFieldValue(cf), optionToSet), changeHolder)

So this script will updated the SelectListA with the option with value AAA when you transit a subtask. If the issue in transition is not a subtask, will do nothing. 

In your example tho, you are trying to update the priority, in that case your script will look like 

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

def issue = issue as MutableIssue

// the issue is not a subtask - do nothing
if (! issue.isSubTask()) {
return
}

def parentIssue = issue.parentObject
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def priority = ComponentAccessor.getComponent(PriorityManager).getPriorities()?.find {it.name == "High"}
def updateIssue = ComponentAccessor.issueManager.getIssueObject(parentIssue.id)

updateIssue.setPriority(priority)
ComponentAccessor.issueManager.updateIssue(currentUser, updateIssue, EventDispatchOption.ISSUE_UPDATED, false)

Hope that helps, 

Thanos

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 30, 2018 in Marketplace Apps

Three tips for boosting your board's efficiency with Story Maps

Trello is one of the most effective tools for driving your sprints. It's customizable for every Agile team and product owners and Scrum masters (SM) love it. However, Agile teams often struggle with:...

818 views 2 9
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you