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

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
Accepted answer
Vasiliy Zverev Community Champion Jan 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")
)

@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 Community Champion Jan 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){
    
}

Thanks @Vasiliy Zverev @Vasiliy Zverev . This works .

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,667 views 18 21
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you