Forums

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

How to define User directory while creating user using script console

Vikrant Yadav
Community Champion
May 1, 2021

Hello Guys,

I am trying to create user in JIRA Internal Directory having ID 1L How can i define directory in Script.

I have tried to define it like :- def directoryId = 1L . But where to put in script so that user get  created in Internal directory instead of creating using default directory which in JIRA Delegated Authentication directory. 

 

import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor

// the username of the new user - needs to be lowercase and unique - required
final String userName = "user"

// The password for the new user - if empty a random password will be generated
final String password = "password"

// The email address for the new user - required
final String emailAddress = "user@jira.com"

// The display name for the new user - required
final String displayName = "New User"

//This display the directory
def directoryId = 1L

// notifications are sent by default, set to false to not send a notification
final boolean sendNotification = false

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def userService = ComponentAccessor.getComponent(UserService)

def newCreateRequest = UserService.CreateUserRequest.withUserDetails(loggedInUser, userName, password, emailAddress, displayName)
.sendNotification(sendNotification)

def createValidationResult = userService.validateCreateUser(newCreateRequest)
assert createValidationResult.isValid() : createValidationResult.errorCollection

userService.createUser(createValidationResult)

 

Script Source :- https://library.adaptavist.com/entity/create-a-user-in-jira

1 answer

1 accepted

3 votes
Answer accepted
Hana Kučerová
Community Champion
May 2, 2021

Hi @Vikrant Yadav ,

according to this documentation you should be able to do something like:

def newCreateRequest = UserService.CreateUserRequest
.withUserDetails(loggedInUser, userName, password, emailAddress, displayName)
.inDirectory(directoryId)

.sendNotification(sendNotification)

Please try, thank you.

Vikrant Yadav
Community Champion
May 2, 2021

@Hana Kučerová Thanks a lot , it works.. ..great :)

Like Hana Kučerová likes this

Suggest an answer

Log in or Sign up to answer