Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×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?
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Nic Brough -Adaptavist- How does it work when I want to copy Component lead to a custom user field?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In the code given above, change it to write to a custom field instead of the assignee of the subtask.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It is a single component field as we have a validation for it. Moderator is read-only text field.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User-picker field should also work as we want to use the field only for view purpose.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Jenna Davis, any references appreciated.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.
Register Now
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.