Assign Component Lead to User Field Edited

I am looking for a solution to execute the below actions on a transition when Component field gets updated.

1. Assign the lead of selected component to a custom field "Moderator"
2. Assignee of all subtasks should be updated to selected Component lead except for subtasks that are in Testing and Close status. How to handle this requirement?

2 answers

1 accepted

This widget could not be displayed.

Hi Srinivas,

I agree with @Nic Brough [Adaptavist] that making the "Moderator" field a user-picker field would simplify things a bit. To set the "Moderator" field to the component lead assuming there is only one component, the script would look something like this:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue as Issue

def
userManager = ComponentAccessor.getUserManager()

def
componentManager = ComponentAccessor.getProjectComponentManager()
def componentLead = userManager.getUserByKey(componentManager.findComponentsByIssue(issue)[0].getLead())

def
customFieldManager = ComponentAccessor.getCustomFieldManager()
def moderatorField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "Moderator"}
moderatorField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(moderatorField), componentLead), new DefaultIssueChangeHolder())

I am using this in a listener that fires on "Issue Updated" and "Issue Created" events.

However, could you clarify if you want to use a listener? Do you want the script to run every time someone updates an issue or do you want the script only to run on certain transitions?

Hi Joshua,

I have a specific transition to update the component field but how can we update the subtasks.

2. Assignee of all subtasks should be updated to selected Component lead except for subtasks that are in Testing and Close status. How to handle this requirement?

Hi Srinivas,

I tested this script below in my instance as a listener. Whenever the parent issue gets updated, it will get all of the subtasks from the issue and set their assignee to the parent issue's component lead. I added an "if" statement to prevent the script from updating subtask issues that are in "Testing" or "Close" status.

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.issueManager
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def issue = event.issue as Issue
def userManager = ComponentAccessor.getUserManager()
def componentManager = ComponentAccessor.getProjectComponentManager()
def componentLead = userManager.getUserByKey(componentManager.findComponentsByIssue(issue)[0].getLead())
issue.getSubTaskObjects().each { subtask ->
if (subtask.getStatus().name != "Close" && subtask.getStatus().name != "Testing"){
subtask.setAssignee(componentLead)
issueManager.updateIssue(currentUser, subtask, EventDispatchOption.DO_NOT_DISPATCH, false)
}
}

Note: you may get static type checking errors in your listener, but you can ignore the errors as the script will still run.

This widget could not be displayed.
Jenna Davis Community Champion Nov 10, 2017

Hello, 

I think you can do this in a post function, but I have a few questions about your implementation first. 

First off, are there going to be (or could there be) multiple components on your issue? If so, how would you like to handle assigning everything?

Second, what kind of field is Moderator, just to check?

Jenna

It is a single component field as we have a validation for it. Moderator is read-only text field.

  

If "moderator" is a text field, what should it copy from the user's account?  Display name or login are the two obvious ones, but possibly email too.

If you're going to use the field again for looking at who the moderator was in code, you're going to have to do a lot of extra work to look the user up.

I would use a user-picker field myself, that simplifies your scripting and retains all the functions of a user field.  If you don't put it on any screen apart from "view", it will be read-only to users.

User-picker field should also work as we want to use the field only for view purpose.

Hi @Jenna Davis, any references appreciated.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

175 views 1 3
Join discussion

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