Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Post-function script that adds user to a group

QTR
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 25, 2019

Hi,

 

Perhaps someone can help me with following, I'm trying to create a transition post-function that adds user to a group (from a user picker custom field). However, when running below script...

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def groupManager = ComponentAccessor.getGroupManager()
def user = (ApplicationUser) ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Field Name").getValue(issue)

def group = groupManager.getGroup("Group1")

groupManager.addUserToGroup(user,group)

...I get this error

019-10-24 22:36:14,574 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-10-24 22:36:14,575 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: ISSUE-66, actionId: 11, file: <inline script>
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[user@user.com)]' with class 'java.util.ArrayList' to class 'com.atlassian.jira.user.ApplicationUser' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.atlassian.jira.user.ApplicationUser(com.atlassian.jira.user.DelegatingApplicationUser)
 at Script74.run(Script74.groovy:6 

1 answer

0 votes
Jack Nolddor _Sweet Bananas_
Atlassian Partner
October 25, 2019

Hi QTR,

The problems is in:

ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Field Name").getValue(issue)

since this doesn't return an (ApplicationUser) but a List<ApplicationUser> you must then ensure that this value isn't return as null and iterate over each element calling the groupManager.addUserToGroup(user,group) for each eleement to add it to the desired group.

 

Regards

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events