Change user properties from scriptrunner custom field

Vladimir Fed January 4, 2020

Hi community!

With help of ScriptRunner I created custom filed(Text Field multi-line) that shows the user properties of the issue reporter

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.UserPropertyManager

UserPropertyManager userPropertyManager = ComponentAccessor.getUserPropertyManager()

String propValue = null


propValue = userPropertyManager.getPropertySet(issue.reporter).getString('jira.meta.More')

return propValue

Is it possible to add a function for changing properties in this field, and how can this be done?
I have little knowledge in Scriptrunner, Thanks in advance.

 

1 answer

0 votes
brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 4, 2020

Hi @Vladimir Fed ,

A. If I understood it correctly, you want to save the reporter's user property in your newly created custom field?

OR 

B. Whatever the value of the newly created custom field it will save it to the user property of the issue reporter?

 

Vladimir Fed January 4, 2020

Hi @brbojorque 

Probably option B

Now the custom field returns the user properties that I added in user management (ex. Key = More)
I want the user’s properties to be changed through the current custom field and saved, even if they are already specified there.

brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 4, 2020

Hi @Vladimir Fed ,

Here's the ScriptRunner Listener code

Remove the old custom field and create a new custom field from Jira.

Make sure it will listen to Issue Created and Issue Updated

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.UserPropertyManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser

UserPropertyManager userPropertyManager = ComponentAccessor.getUserPropertyManager()

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def userPropertiesField = customFieldManager.getCustomFieldObjectByName("User Properties") //Multi line custom field

MutableIssue issue = (MutableIssue) event.issue

//Saving the property Preferences
userPropertyManager.getPropertySet(issue.reporter).setString('jira.meta.more', issue.getCustomFieldValue(userPropertiesField) as String)

//Reading the property
String propValue
propValue = userPropertyManager.getPropertySet(issue.reporter).getString('jira.meta.more')

//Saving the property to Custom Field
issue.setCustomFieldValue(userPropertiesField, propValue)
ApplicationUser applicationUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issueManager.updateIssue(applicationUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

Out of curiosity what is the reasoning behind this requirement? 

-

Vladimir Fed January 5, 2020

Hi, @brbojorque 

Thanks a lot, but code doesn't work.

Screenshot_2.jpgScreenshot_3.jpgThese are support B2B requirements for Service Desk.

They need the ability to change the contact details of customers, provided that they have the rights of only an  SD agent

brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 5, 2020

Hi @Vladimir Fed ,

You can't actually change the User data by just changing the preferences. 

E.g Username, Last name, first name.

I believe this solution is not for you.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events