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
Community showcase
Posted Oct 11, 2018 in Marketplace Apps

You + one app + a desert island...

Hi all! My name is Miles and I work on the Marketplace team. We’re looking for better ways to recommend and suggest apps that are truly crowd favorites, so of course we wanted to poll the Community. ...

3,496 views 6 6
Join discussion

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