Help setting User Property dynamically using a groovy script

We have 289 users currently, and have been told that for integration purposes we need to now create a User Property for each of them that holds their email alias without the @domain.com piece due to special character issues. Rather than have to update all 289 of them manually one by one, I'm hoping to script something up.

This is what I've gotten so far based on what I've found in forums and online. I believe my issue is related to the fact that I'm not getting an ApplicationUser as I iterate through the userList collection, and then I have an error on the getPropertySet() that I can't decipher. 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.user.util.UserManager
UserUtil userUtil = ComponentAccessor.getUserUtil()

def userPropertyManager = ComponentAccessor.getUserPropertyManager()
def userManager = ComponentAccessor.getUserManager()
def userList=[UserManager.getAllApplicationUsers()]
def email

userList.each { thisUser ->
    email = thisUser.getEmailAddress()
    userPropertyManager.getPropertySet(thisUser.getName()).setString("jira.meta.Alias",email.substring(0, email.indexOf('@')))
}

and the error message: 

2017-06-23 17:33:36,795 WARN [common.UserScriptEndpoint]: Script console script failed: 
groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.user.util.UserManager.getAllApplicationUsers() is applicable for argument types: () values: []
	at Script80.run(Script80.groovy:8)

1 answer

1 accepted

0 votes
Accepted answer

Hi Blake.

I think you should do this:

def userPropertyManager = ComponentAccessor.getUserPropertyManager()
def userManager = ComponentAccessor.getUserManager()
def userList=UserManager.getAllApplicationUsers()
def email

userList.each { thisUser ->
    email = thisUser.getEmailAddress()
    userPropertyManager.getPropertySet(thisUser.getName()).setString("jira.meta.Alias",email.substring(0, email.indexOf('@')))
}

 Without the brackets around the user manager, the script will give you a red at the start of the closure, but in execution time it should work properly. If you want to avoid the red you could do this:

def userPropertyManager = ComponentAccessor.getUserPropertyManager()
def userManager = ComponentAccessor.getUserManager()
Collection<ApplicationUser> userList=UserManager.getAllApplicationUsers()
def email

userList.each { thisUser ->
    email = thisUser.getEmailAddress()
    userPropertyManager.getPropertySet(thisUser.getName()).setString("jira.meta.Alias",email.substring(0, email.indexOf('@')))
}

Hope this helps!

Cheers!

Dyelamos

 

Thanks Dyelamos! I've tried both methods, but unfortunately am still getting the same error (for both methods):

Logs:

2017-06-26 10:32:40,572 WARN [common.UserScriptEndpoint]: Script console script failed: 
groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.user.util.UserManager.getAllApplicationUsers() is applicable for argument types: () values: []
 at Script95.run(Script95.groovy:8)

Result: 

Result error.jpg

Oh boy :D

I just realised, that you are calling

UserManager, (first letter is a capital, which is a global variable)

insted of 

userManager, (first letter undercase) which is the variable you get line 3.

Apologies for missing that. Your script should look like this:

def userPropertyManager = ComponentAccessor.getUserPropertyManager()
def userManager = ComponentAccessor.getUserManager()
def userList=userManager.getAllApplicationUsers()
def email

userList.each { thisUser ->
    email = thisUser.getEmailAddress()
    userPropertyManager.getPropertySet(thisUser.getName()).setString("jira.meta.Alias",email.substring(0, email.indexOf('@')))
}

Cheers!

Dyelamos

Thanks Dyelamos - that would have driven me crazy!

I was able to finally get it to work with your script by changing

userPropertyManager.getPropertySet(thisUser.getName()).

 to

userPropertyManager.getPropertySet(thisUser)

 

thanks again for your help!

No problem. Please accept my answer so that people know this has been answered!

Cheers

Dyelamos

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,153 views 0 8
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you