Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Add users from UserPicker Custom field to UserGroup using Scriptrunner

Florian Baur August 20, 2024

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.ComponentAccessor

def userUtil = ComponentAccessor.userUtil
def userManager = ComponentAccessor.userManager
final 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?

 

1 answer

0 votes
Stefan Stadler
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.
August 21, 2024

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.ApplicationUser def userManager = ComponentAccessor.userManager final 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") } }


Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
9.4.15
TAGS
AUG Leaders

Atlassian Community Events