Add users from UserPicker Custom field to UserGroup using Scriptrunner
Asked by Florian Baur:

Hi, you all

I will probably start biting into the table, as I have not reached the point of adding users from a user picker custom field to a user group using Scriptrunner, which is triggered by an automation rule.

    import com.atlassian.jira.component.ComponentAccessordef userUtil = ComponentAccessor.userUtildef userManager = ComponentAccessor.userManagerfinal String groupName = "CSE_QM_InternalAuditor"def group = ComponentAccessor.groupManager.getGroup(groupName)final List<String> userToAdd = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("User"))userToAdd.each {    def usertoaddhere = userManager.getUserByName(it)    if (!usertoaddhere) {        log.warn("User: $it doesn't exist")        return    }    if (ComponentAccessor.getGroupManager().getGroupsForUser(usertoaddhere).contains(group)) {        log.warn("User: $usertoaddhere.username already in the group: $groupName")        return    }    if(!group) {        log.warn("Group: $groupName doesn't exist")    } else {        userUtil.addUserToGroup(group, usertoaddhere)        log.warn("User: $usertoaddhere.username added to the $groupName")    }}

I get the following error message:

Script function failed on Automation for Jira rule: AddUserToCSE_QM_InternalAuditor, file: \<inline script\>, error: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.user.util.DefaultUserManager.getUserByName() is applicable for argument types: (com.atlassian.jira.user.DelegatingApplicationUser) values: \[dobrkl(JIRAUSER23000)\] Possible solutions: getUserByName(java.lang.String), getUserById(java.lang.Long), getUserByKey(java.lang.String), getUserState(com.atlassian.jira.user.ApplicationUser)

Does anybody have an idea why and how I can fix that?


Answers:
author: Stefan Stadler
body: Hi Florian,

the problem I believe consists of actually two problems:

1. Getting a custom field value is done as following:

```
String fieldName = "User"
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObjects(issue).find {CustomField it -> it.name == fieldName}
def cFieldValue = issue.getCustomFieldValue(cField)
```

2. That being said, if your custom field is of type user picker, it will most likely return a List of ApplicationUser objects, so mapping them to a List of Strings will not work.

Try using List\<ApplicationUser\> instead of List\<String\>.

In consequence, you can skip getting a user from its name as you already have the user objects available.

Lastly: I am not sure if userUtil will work well, but I have just verified the following code to be working fine:  

    import com.atlassian.jira.component.ComponentAccessor
    import com.atlassian.jira.user.ApplicationUserdef userManager = ComponentAccessor.userManagerfinal String groupName = "CSE_QM_InternalAuditor"def group = ComponentAccessor.groupManager.getGroup(groupName)

    String fieldName = "User"
    def issueManager = ComponentAccessor.getIssueManager()
    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def cField = customFieldManager.getCustomFieldObjects(issue).find {CustomField it -> it.name == fieldName}
    final List<ApplicationUser> userToAdd = issue.getCustomFieldValue(cField)userToAdd.each {    def usertoaddhere = it    if (!usertoaddhere) {        log.warn("User: $it doesn't exist")        return    }    if (ComponentAccessor.getGroupManager().getGroupsForUser(usertoaddhere).contains(group)) {        log.warn("User: $usertoaddhere.username already in the group: $groupName")        return    }    if(!group) {        log.warn("Group: $groupName doesn't exist")    } else {
            ComponentAccessor.groupManager.addUserToGroup(usertoaddhere,group)        log.warn("User: $usertoaddhere.username added to the $groupName")    }}



Tags: data-center

jira-data-center

jira-server

server