Skip to main content
Example scripts
arrow icon
To homepage
Confluence
Data centre icon
Data Center

Update User Emails In Bulk

Tags
Created 1 year ago, Updated 1 month(s) ago
App in script
ScriptRunner For Confluence
ScriptRunner For Confluence
by Adaptavist
Compatibility
compatibility bullet
Confluence (7.15 - 8.6)
compatibility bullet
ScriptRunner For Confluence (7.10.0)
Language |
groovy
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.user.UserAccessor
import com.atlassian.user.impl.DefaultUser
import com.onresolve.scriptrunner.parameters.annotation.ShortTextInput

@ShortTextInput(label = 'File path', description = 'The file path to your CSV file')
String csvFilePath
assert csvFilePath

def userAccessor = ComponentLocator.getComponent(UserAccessor)
// Edit the file path below to your user csv file path
def file = new File(csvFilePath)
def csvMapList = []

file.eachLine {
    // Read each line of a CSV file to form a user map
    line ->
        def columns = line.split(",")
        def tmpMap = [:]
        tmpMap.putAt("username", columns[0])
        tmpMap.putAt("original_email", columns[2])
        csvMapList.add(tmpMap)
}
log.debug(csvMapList)

def accountsUpdated = 0
csvMapList.each {
    // Iterate through each user in map and update user emails
    map ->
        def userName = map["username"] as String
        def originalEmail = map["original_email"] as String
        // Edit new_email to the new domain you want to use
        def newEmail = originalEmail.split('@')[0].concat("@new_email.com")

        if (userAccessor.exists(userName)) {
            def originalUser = userAccessor.getUserByName(userName)
            def modifiedUser = new DefaultUser(originalUser)
            modifiedUser.setEmail(newEmail)
            userAccessor.saveUser(modifiedUser)
            if (userAccessor.getUserByName(userName).getEmail() == newEmail) {
                log.debug("Successfully updated " + userName + "'s email from " + originalEmail + " to " + newEmail)
                accountsUpdated++
            } else {
                log.debug(userName + "'s email was not successfully updated.")
            }
        } else {
            log.debug("User " + userName + " was not found.")
        }
}

log.debug("User update completed. Successfully updated " + accountsUpdated + " accounts.")
Having an issue with this script?
Report it here