Change subtask assignee based on parent WITHOUT post function

Usage: I have an issue with a dozen subtasks. I wan to change the assignee of the issue to someone else and have all of the subtasks automatically update as well, however, I do not want to create a workflow item to do this. Our workflow is already pretty complex as it is.

I don't really have the knowledge to create my own plugin or figure out the event listeners. My thinking was to use the script runner to have the escalation service work off of the following JQL query. Honestly I'd rather not use the service since its sitting there checking every x number of minutes, but if it works, I'll take it:

('Subtask Follow' = 'Yes') AND (Status != 'Closed') AND ('assignee' CHANGED)

Subtask follow is basically a field that lets the user decide if they want the assignement change to affect all the subtasks. Here's the gist of the script I envisioned (that obviously doesn't work..):

issue.subtask.assignee == issue.assignee

I've tried a variety of other scripts based on my searches, but I just don't know enough to really understand what the problem is. Seems like this should be simple enough and useful enough...

2 answers

1 accepted

package com.atlassian.jira.event.listeners

import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import org.apache.log4j.Category
import org.apache.log4j.Level
import org.apache.log4j.Logger

public class SubtaskAutoAssignListener extends AbstractIssueEventListener {
    Category log = Category.getInstance(SubtaskAutoAssignListener.class)
 
    @Override
    void workflowEvent(IssueEvent event) {
        log.error("\n \n \n \n ---- Assignee Updater starts -----  \n \n \n \n \n")
		CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
		def customField_Follow = customFieldManager.getCustomFieldObjectByName( "Subtask Follow" )
		def subtaskFollow = event.issue.getCustomFieldValue(customField_Follow)
		log.error("\n \n \n Assign subtask to parent assignee?: $subtaskFollow \n \n")
		if ("$subtaskFollow" == "Yes") {
			if (!event.issue.isSubTask()){
				def issueAssigneeId = event.issue.getAssigneeId()
				log.error("\n \n \n Parent Issue Assignee: $issueAssigneeId \n")

				IssueManager issueManager = ComponentAccessor.getIssueManager()
				UserManager userManager = ComponentAccessor.getUserManager()

				event.issue.getSubTaskObjects().each { t ->
					log.error("\n \n \n Subtask ID: $t \n \n \n")
					def subtaskAssignee = t.getAssigneeId()
					log.error("\n \n \n Current subtask assignee: $subtaskAssignee \n \n \n")
					if (!subtaskAssignee.equals(issueAssigneeId)) {
						MutableIssue myIssue = t
						myIssue.setAssigneeId(issueAssigneeId)
						issueManager.updateIssue(userManager.getUser(issueAssigneeId),myIssue,EventDispatchOption.DO_NOT_DISPATCH, false)
						def newSubtaskAssignee = myIssue.getAssigneeId()
						log.error("\n \n \n New subtask assignee: $newSubtaskAssignee \n \n \n")
					}
				}
			}
		}
		log.error("\n \n \n \n ---- Assignee Updater Ends -----  \n \n \n \n \n")
    }
}

That seems to work.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

578 views 6 12
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