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

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

0 votes
Thanos Batagiannis [Adaptavist] Community Leader Dec 05, 2017

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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Marketplace Apps & Integrations

Webinar alert! An IT Admin's Guide to Securing Collaboration at Scale

Hello, Atlassian Community! My name is Dave Meyer and I'm a Principal Product Manager at Atlassian. I wanted to give this community a heads up about an upcoming Webinar that might be of interest...

188 views 2 7
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