Is their any way to add all 100 users in a below script, so that all users created in one go OR i need to run script everytime for each users.Please suggest
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"
// 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)
Source :- https://library.adaptavist.com/entity/create-a-user-in-jira
@Stephen_Wright @Nic_Brough__Adaptavist_ @Kristian Walker (Adaptavist) @Adrian_Stephen @Derek_Fields @Ravi_Sagar__Adaptavist_
Please help
Thanks
Vikrant yadav
@Vikrant Yadav I tried to adjust my answer and it dissapeared :(
The fix is:
def users = [
[userName: "superman", displayName: "Superman", password: "superman", emailAddress: "superman@gmail.com"],
[userName: "batman", displayName: "Batman", password: null, emailAddress: "batman@gmail.com"]
]
I used "{" / "}" instead of "[" / "]". Give it a try and let me know :)
For others:
It can be simply done using "for" loop in groovy / Java:
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
def users = [
[userName: "superman", displayName: "Superman", password: "superman", emailAddress: "superman@gmail.com"],
[userName: "batman", displayName: "Batman", password: null, emailAddress: "batman@gmail.com"]
]
// 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)
for(def user: users){
def newCreateRequest = UserService.CreateUserRequest.withUserDetails(loggedInUser, user.userName, user.password, user.emailAddress, user.displayName)
.sendNotification(sendNotification)
def createValidationResult = userService.validateCreateUser(newCreateRequest)
if(createValidationResult.isValid()){
userService.createUser(createValidationResult)
} else {
log.error createValidationResult.errorCollection
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
hi @Vikrant Yadav these errors are "ok", you are not using TYPES in groovy, but "def" so console for example does not know property "errorCollection" on "createValidationResult" as property "createValidationResult" does not have TYPE defined.
Real errors will appear when you try to execute the script.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Vikrant Yadav my first answer appeared again, try the code in the first example, I will delete the second one to clean this theme up
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Martin Bayer [MoroSystems, s.r.o.] first response also giving error. I already attached the screenshot
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Vikrant Yadav I fixed braces after you reported error { -> [ and } -> ] as there was error with this. Can you check it once again, please?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Martin Bayer [MoroSystems, s.r.o.] in this script where braces do i need to changes, can you please share the correct script.
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
def users = [
[userName: "superman", displayName: "Superman", password: "superman", emailAddress: "superman@gmail.com"],
[userName: "batman", displayName: "Batman", password: null, emailAddress: "batman@gmail.com"]
]
// 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)
for(def user: users){
def newCreateRequest = UserService.CreateUserRequest.withUserDetails(loggedInUser, user.userName, user.password, user.emailAddress, user.displayName)
.sendNotification(sendNotification)
def createValidationResult = userService.validateCreateUser(newCreateRequest)
if(createValidationResult.isValid()){
userService.createUser(createValidationResult)
} else {
log.error createValidationResult.errorCollection
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Martin Bayer [MoroSystems, s.r.o.] still this script showing error , but it's working. creating users. Thanks Mate
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great to hear it's working for you, it will show errors until you will not used TYPED variables. But I guess it is one time operation so it will not affect your Jira configuration experience :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, yes you can, you need collection of users data:
It can be for example
def users = [[username: "superman", displayname: "Superman", password: "superman", email: "superman@gmail.com"], [username: "batman", displayname: "Batman", password: null, email: "batman@gmail.com"]]
Then you need to adjust script little bit:
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
def users = [
[userName: "superman", displayName: "Superman", password: "superman", emailAddress: "superman@gmail.com"],
[userName: "batman", displayName: "Batman", password: null, emailAddress: "batman@gmail.com"]
]
// 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)
for(def user: users){
def newCreateRequest = UserService.CreateUserRequest.withUserDetails(loggedInUser, user.userName, user.password, user.emailAddress, user.displayName).sendNotification(sendNotification)
def createValidationResult = userService.validateCreateUser(newCreateRequest)
def valid = createValidationResult.isValid()
if(valid){
userService.createUser(createValidationResult)
}else{
log.error createValidationResult.errorCollection
}
}
I didn't test the script so give it a try and let me know what is the result.
Note:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey @Martin Bayer [MoroSystems, s.r.o.] thanks for the response. But getting an error , i used the script shared by you :-
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.