Forums

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

Write code to change user email address unable to resolve class UserAccessor

Mouna Hammoudi
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 15, 2024

I am trying to write code to change a user email address in Jira and I am getting the following error for my last import 

unable to resolve class com.atlassian.confluence.user.UserAccessor

 

Anyone knows how this can be fixed? I am using Jira Service Management 5.12.11 and Jira Software 9.12.11. Anyone knows how to fix the problem?

import com.atlassian.sal.api.component.ComponentLocator

import com.atlassian.jira.issue.fields.CustomField;

import com.atlassian.jira.issue.CustomFieldManager;

import com.atlassian.jira.user.util.UserManager;

import com.onresolve.scriptrunner.parameters.annotation.ShortTextInput

import com.atlassian.jira.bc.user.DefaultUserService

import com.atlassian.jira.user.util.UserManager

import com.atlassian.confluence.user.UserAccessor

@ShortTextInput(label = 'File path', description = '\\itrac-tst\\mouh-share\\newMailAddress.txt')

String 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 =  UserManager.getUser(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.")

 

 

1 answer

0 votes
Fabio Racobaldo _Herzum_
Community Champion
March 2, 2025

Hi @Mouna Hammoudi ,

in the import section I see UserAccessor imported from Confluence and it is wrong!

You should UserUtil api instead (https://docs.atlassian.com/software/jira/docs/api/9.4.0/com/atlassian/jira/user/util/UserUtil.html#userExists-java.lang.String-)

Hope this helps,

Fabio

Suggest an answer

Log in or Sign up to answer