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

This widget could not be displayed.

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published Jul 25, 2018 in Marketplace Apps

Jira Cloud and Bitbucket Cloud Integration with Microsoft Teams

One of the newest products in the Microsoft family - Microsoft Teams,  is a chat-based hub for teamwork that integrates all the people, content, and tools your team needs to be more engaged and ...

727 views 0 3
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