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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

236 views 0 6
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