You get the value from the issue object
def groupName = issue.getCustomFieldValue(groupNameToAddCf)
Then if you want to get a group object
def groupObj = ComponentAccessor.groupManager.getGroup(groupName)
Put all together:
import com.atlassian.jira.component.ComponentAccessor
def groupNameCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("HR")[0]
def userNameCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("User name")[0]
def userName = issue.getCustomFieldValue(userNameCf) as String
def groupName = issue.getCustomFieldValue(groupNameCf) as String
def user = ComponentAccessor.userManager.getUserByName(userName)
def group = ComponentAccessor.groupManager.getGroup(groupName)
if (!group) {
addError("Group doesnt exists")
return
}
if (!user) {
addError("User doesnt exists")
return
}
if (ComponentAccessor.groupManager.getGroupsForUser(user).contains(group)) {
addError("User: $userName already in $groupName")
return
}
ComponentAccessor.groupManager.addUserToGroup(user, group)
addMessage(" $userName added to $groupName")
@Peter-Dave Sheehan
"User name doesnt exists". At this moment script cannot identify a user in "User name" custom field (User picker form). I'll try to connect half of old code with your and "User name doesnt exists"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It wasn't clear to me what type of field the User Name field was. So I assumed text.
If it's a user picker, then you don't need to use the UserManager to get the user. The getFieldValue will return an application user. Then if the user is empty, it's not because it doesn't exist in Jira, it's because they user forgot to enter it (which can be fixed by making the field required).
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
def groupNameCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("HR")[0]
def userNameCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("User name")[0]
def user = issue.getCustomFieldValue(userNameCf) as ApplicationUser
def groupName = issue.getCustomFieldValue(groupNameCf) as String
def group = ComponentAccessor.groupManager.getGroup(groupName)
if (!group) {
addError("Group doesnt exists")
return
}
if (!user) {
addError("User was not entered")
return
}
if (ComponentAccessor.groupManager.getGroupsForUser(user).contains(group)) {
addError("User: $userName already in $groupName")
return
}
ComponentAccessor.groupManager.addUserToGroup(user, group)
addMessage(" $userName added to $groupName")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.