Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to check in listener that sub-task's parent was changed?

Barys Harbacheuski April 2, 2020

I have a task: copy components from parent to sub-task when sub-task's parent changed.

Example:

I have Parent 1 with component ABC and its sub-task 1 with the same component ABC and Parent 2 with component DEF. I move sub-task 1 to Parent 2, so sub-task 1's component should change to DEF.

 

In listeners I usually use event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == 'fieldName' } so I wonder if there is a field in sub-tasks that contains parent issue value so I can check if this field changed.

If no, is there another way to implement it?

1 answer

1 accepted

0 votes
Answer accepted
Barys Harbacheuski April 3, 2020

Found the field name myself :) it is "Parent Issue"

Maybe it will help someone, the code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption
// current issue
def issue = event.issue as MutableIssue
if (issue.getIssueType().getName() == "Sub-task") {
// the name of the field to check
final String fieldName = "Parent Issue"
// user that will make updates
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
// check if a right field was updated
def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find
{it.field == fieldName}
if (change) {
def issueManager = ComponentAccessor.getIssueManager()
// get components of parent issue
def components = issue.getParentObject().getComponents()
issue.setComponent(components)
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
} else return
} else return

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events