How do I create a script field in script runner that lists all of the users in a role for that project?

Timur Rozenfeld October 21, 2014

I need to create a script field in script runner that displays all of the users within a specific role for that project. How should this be coded?

Thanks.

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Andreas Ebert
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.
October 21, 2014

At some place you will have to use ProjectRoleManager#getProjectRoleActors() and DefaultRoleActors#getApplicationUsers(). Like this:

ProjectRoleManager projectRoleManager;
ProjectRoleActors roleActors = projectRoleManager.getProjectRoleActors(myProjectRole, myProject);
Set<ApplicationUsers> usersInRole = projectActors.getApplicationUsers();
// or for JIRA 5: Set<User> usersInRole = projectActors.getUsers();

For scripted fields itself take a look at Script Runner's docu.

0 votes
Timur Rozenfeld October 22, 2014

Thanks for your input. This code returns the correct list of users, but also an error:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.user.ApplicationUsers
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
 
def projectRoleId = 10300  // ID of Automatic Watcher project role
def role = projectRoleManager.getProjectRole(projectRoleId)
def actors = projectRoleManager.getProjectRoleActors(role, issue.projectObject)
def usersInRole = actors.getApplicationUsers();
return usersInRole;

Here is the error I get:

The indexer for this field expects a java.lang.String but the script returned a java.util.HashSet - this will cause problems.

Andreas Ebert
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.
October 22, 2014

You probably have the wrong Searcher set in your scripted field. Try the Multi User Picker Searcher.

JamieA
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.
October 22, 2014

Perhaps you don't need it indexed at all, in which case choose None. If you do need it indexed you may need to convert between different types of users... try without indexer first.

Timur Rozenfeld October 23, 2014

Andreas was right, I had the searcher for the custom field set to Text Searcher instead of the multi-user searcher. Once I changed it to multi-user searcher, the error went away. Thanks.

TAGS
AUG Leaders

Atlassian Community Events