Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

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

Extract user value from jira custom field and add it to group

Abyakta Lenka January 12, 2017

I am trying to get user name from a JIRA custom filed and put it into a group . how can i achieve this using groovy or any postfunction in jira .I am trying to do this using Workflow post-function . Any other solution is welcome .

Eg : field A = user1 

i want to add "user1" to group "Jiraxyz" .

help 

Abyakta

1 answer

1 accepted

1 vote
Answer accepted
Vasiliy Zverev
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.
January 13, 2017

Here is code for script postfunction provided by ScriptRunner plugin:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.user.ApplicationUser

Issue issue
GroupManager groupManager = ComponentAccessor.getGroupManager();
groupManager.addUserToGroup(
        ((ApplicationUser) issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("field name"))).getDirectoryUser()
        , groupManager.getGroup("groupName")
)
Abyakta Lenka January 14, 2017

@Vasiliy Zverev @Vasiliy Zverev above code is giving error .

failed on issue: ABCD-5989, actionId: 11, file: <inline script>

java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object

 at Script23.run(Script23.groovy:9)


What i did :

I pasted the above code with groupName and customfield change in the postfunction.

Let me know if i am doing anything wrong


Abyakta

Vasiliy Zverev
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.
January 15, 2017

This error namely means that custom field is empty to given issue. Here is updated code to fix this case:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.user.ApplicationUser

GroupManager groupManager = ComponentAccessor.getGroupManager();
try {
    groupManager.addUserToGroup(
            ((ApplicationUser) issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("field name"))).getDirectoryUser()
            , groupManager.getGroup("groupName")
    )
}
catch (NullPointerException e){
    
}
Abyakta Lenka January 16, 2017

Thanks @Vasiliy Zverev @Vasiliy Zverev . This works .

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Upcoming Jira Events