Forums

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

Need Help with the Scriptrunner Script

Jayesh September 23, 2025

I need to create a script using scriptrunner to bulk create the users, using the below but facing issues,

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

def userService = ComponentAccessor.getComponent(UserService)
def userManager = ComponentAccessor.getUserManager()
def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def usersToCreate = [
    [username: "user1", password: "Password@123", email: "user1@example.com", fullName: "User One"],
    [username: "user2", password: "Password@123", email: "user2@example.com", fullName: "User Two"],
    [username: "user3", password: "Password@123", email: "user3@example.com", fullName: "User Three"],
]

usersToCreate.each { userData ->
    def createUserRequest = UserService.CreateUserRequest.withUserDetails(
        loggedInUser,
        userData.username,
        userData.password,
        userData.email,
        userData.fullName
    )
    createUserRequest.withDirectoryId(1L) // Use correct internal directory ID

    def result = userService.createUser(createUserRequest)

    if (result.isSuccess()) {
        log.info "Successfully created user: ${userData.username}"
    } else {
        log.warn "Failed to create user: ${userData.username}. Errors: ${result.errorCollection}"
    }
}


Any help will be appreciated.

1 answer

0 votes
Samuel Gatica _ServiceRocket_
Community Champion
September 23, 2025

Hi  @Jayesh 

What's the error you are getting?

Can you confirm the Directory ID?- Go to Administration → User Management → User Directories (Server/DC) to confirm the directory ID (hover the link or check DB)

 

Best regards

Sam

Jayesh September 23, 2025

@Samuel Gatica _ServiceRocket_ 

Thanks for the reply, I was getting error in CreateUser function, later searching I found a new class in scriptrunner KB articles for user creation and used them, below is my code

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.crowd.manager.directory.DirectoryManager
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.user.util.UserManager
import com.atlassian.crowd.embedded.api.Directory
import com.atlassian.jira.exception.CreateException
import com.atlassian.jira.application.ApplicationKeys

// Define the path to your CSV file
def csvFilePath = "/opt/atlassian-shared/tmp/users.csv"

// Define the directory ID. '10000' is the default for Jira Internal Directory.
def directoryId = 1L


// Get necessary managers. Cast them to their correct interface types.
def userManager = ComponentAccessor.userManager as UserManager
def groupManager = ComponentAccessor.getComponent(GroupManager) as GroupManager
def directoryManager = ComponentAccessor.getComponent(DirectoryManager) as DirectoryManager

// Get the directory object
def Directory directory = directoryManager.findDirectoryById(directoryId)
if (!directory) {
    log.error("Directory with ID ${directoryId} not found.")
    return
}

// Read and parse the CSV file line by line
def csvFile = new File(csvFilePath)
if (!csvFile.exists()) {
    log.error("CSV file not found at: ${csvFilePath}")
    return
}

csvFile.eachLine { line, lineNumber ->
    if (line.trim().isEmpty()) {
        return
    }

    def parts = line.split(",").collect { it.trim() }

    if (parts.size() < 3) {
        log.warn("Skipping line ${lineNumber}: '${line}'. Not enough fields.")
        return
    }

    def username = parts[0]
   // def password = parts[1]
    def email = parts[1]
    def fullName = parts[2]

    try {

        Users.create(username, email, fullName)
        log.info("Successfully created user: ${username}")
        def group = Groups.getByName('jira-users')
        group.add(username)
       
   
    } catch (CreateException e) {
        log.error("Error creating user '${username}': ${e.message}")
    } catch (Exception e) {
        log.error("Error processing user '${username}' on line ${lineNumber}: ${e.message}")
    }
}

log.info("Script execution complete.")


I need a help now to create a web panel in usermanagement for bulk create users, where user can upload the csv and able to create the users.

In my above script, I have to place the csv file in server and I need to make it for easy for jira admins

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events