Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,368,748
Community Members
 
Community Events
168
Community Groups

Get value from custom field scriptrunner

Good day!
How can i get value from cField "HR"? This is simple text custom field

import
com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Logger
import org.apache.log4j.Level

def groupNametoAddCf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("HR")
def NameCf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("User name")
def Name = issue.getCustomFieldValue(NameCf) as ApplicationUser
def userToAdd = ComponentAccessor.getUserManager().getUserByName(NameCf.name)
def groupName = ComponentAccessor.getGroupManager().getGroup(groupNametoAddCf.name)

// def log = Logger.getLogger("com.acme.CreateSubtask")
// log.setLevel(Level.DEBUG)
// log.debug NameCf

if(!groupName) {
    addError("Group doesnt exists")
} else {
    ComponentAccessor.groupManager.addUserToGroup(userToAdd, groupName)
    addMessage("$userToAdd.username add in $groupName.name")
}

if (!userToAdd) {
    addError("User doesnt exists")
    return
}

if (ComponentAccessor.getGroupManager().getGroupsForUser(Name).contains(groupName)) {
    addError("User: $userToAdd.username already in $groupName.name")
    return
}

1 answer

0 votes

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"

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")
Like Alexandr Trifonov likes this

This works! Thanks!!!

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events