Forums

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

[Scriptrunner] If-condition not ignored when adding/creating user to group

Sngy October 5, 2020

Hello, 

so I have a little script, which checks in the first if-condition, if the user is part of one group. If he is not, he will be created first, and added to the group (through "else). I thought, usually an if-condition will be ignored, if it is not true; but here my code keeps giving me an "Nullpointer Error" for the if-condition, and does not go to the else-condition. 

 

import com.atlassian.jira.component.ComponentAccessor

def groupManager = ComponentAccessor.getGroupManager()
def user = "newuser"
def groupNametoAddconfluence = "confluence-users"
def groupNametoAddjira = "jira-users"
def userToAdd = groupManager.getUserManager().getUserByName(user)
def groupNameToAdd = ComponentAccessor.getGroupManager().getGroup(groupName)
def usernotingroup = ['jira-users', 'confluence-users'].any {groupManager.isUserInGroup(user, it)}

if(!usernotingroup) {
ComponentAccessor.groupManager.addUserToGroup(groupNametoAddconfluence)

ComponentAccessor.groupManager.addUserToGroup(groupNametoAddjira)


} else {


ComponentAccessor.getUserUtil().createUserNoNotification(user, "", "Mail", "Full Name")
def groupName1 = ComponentAccessor.getGroupManager().getGroup(groupNametoAddconfluence)
def groupName2 = ComponentAccessor.getGroupManager().getGroup(groupNametoAddjira)


ComponentAccessor.groupManager.addUserToGroup(userToAdd,groupName1)

ComponentAccessor.groupManager.addUserToGroup(userToAdd,groupName2)

1 answer

0 votes
Kristian Walker _Adaptavist_
Community Champion
October 5, 2020

Hi Son,

I notice that you have marked this request as relating to ScriptRunner for Jira Cloud but I notice that your code is for ScriptRunner for Jira Server and will not work inside of Jira Cloud and this will not work as Atlassian only provides a rest API in Jira Cloud and does not provide a Java API in the cloud like they do in Jira Server.

You can see more detailed information on the differences between the cloud and server versions inside of our documentation page located here.

If your question relates to Jira Server then I would recommend re tagging this to specify Jira Server over Jira Cloud so that users familar with Jira Server see this question and can assist with it.

I hope this helps,

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer