Assign Component Lead to User Field

srinivasp
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 9, 2017

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?

3 answers

1 accepted

0 votes
Answer accepted
Joshua Yamdogo @ Adaptavist
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 16, 2017

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?

srinivasp
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 17, 2017

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?

Joshua Yamdogo @ Adaptavist
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 17, 2017

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.

0 votes
Ankit Patel July 14, 2020

@Nic Brough -Adaptavist-  How does it work when I want to copy Component lead to a custom user field? 

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 16, 2020

In the code given above, change it to write to a custom field instead of the assignee of the subtask.

0 votes
Jenna Davis
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 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

srinivasp
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 11, 2017

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

  

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 12, 2017

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.

srinivasp
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 12, 2017

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

srinivasp
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 14, 2017

Hi @Jenna Davis, any references appreciated.

Suggest an answer

Log in or Sign up to answer