Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,555,354
Community Members
 
Community Events
184
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
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Sep 15, 2022

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"

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Sep 16, 2022

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
AUG Leaders

Atlassian Community Events