Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,557,079
Community Members
 
Community Events
184
Community Groups

Set value of a Single User Picker based on value of a Single Select List

Edited
Michael Thompson
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.
Jul 15, 2019

Hi folks,

I have a tough issue here and need some assistance coming up with a solution. 

Background

Jira Software 8.2.0
Adaptavist ScriptRunner 5.5.8.1-jira8

I have an inherited Jira Software instance that I have been slowly working on cleaning up, as it outgrew its original purpose long ago.

One of my projects has a Single Select List custom field named TTS Approver. It contains a text list users that can approve issues in the TTS project. I'm trying to get rid of this Single Select List in favor of a new Single User Picker custom field, which will be much easier to manage. Let's call it newApprover for now.

Creating the newApprover field is easy enough, and I've added it to the relevant screens in my test environment. The problem I face now is replacing the old TTS Approver information in existing issues (both resolved and unresolved) with this new field. I will then remove the old field from the screens and rename newApprover as TTS Approver. I'm looking at about 35,000 issues in this project alone.

Problem

I thought I could use ScriptRunner's built-in script Copy custom field values to simply copy the value from TTS Approver to newApprover, but this does not work with User Picker custom fields.

Does anyone know a way to script this such that the text value in TTS Approver = the name property of the newApprover user field, and thus the correct user will be populated in the field?

2 answers

1 accepted

2 votes
Answer accepted
Alejandro Suárez
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.
Jul 16, 2019

Hi @Michael Thompson 

I built a code to solve your problem, you can run it trough the "Script Console" page.

It only works if your TTS Approvers contains an "username" instead o the "Full Name"

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.Query

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

CustomField cfTTSapprover = customFieldManager.getCustomFieldObject(67890L) //Change with the ID of your "TTS Approver" CustomField
CustomField cfNewApprover = customFieldManager.getCustomFieldObject(12345L) //Change with the ID of your "New Aprrover" CustomField

String filter = "\"cf[${cfTTSapprover.getId().toString()}]\" is not EMPTY"
Query query = ComponentAccessor.getComponent(JqlQueryParser).parseQuery(filter)
SearchResults<Issue> search = ComponentAccessor.getComponent(SearchService).search(currentUser, query, new PagerFilter())

String currentUserName

search?.getResults()?.each {
currentUserName ? ComponentAccessor.getUserManager().getUserByName(it.getCustomFieldValue(cfTTSapprover).toString()) : null
cfNewApprover.updateValue(null, it, new ModifiedValue(it.getCustomFieldValue(cfNewApprover), currentUserName), new DefaultIssueChangeHolder())
ComponentAccessor.getComponent(IssueIndexingService).reIndex(it)
}
Alejandro Suárez
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.
Jul 16, 2019

PS. If you have the "Full Names" instead the "user names" in the options of your TTS Select List, if you give me the match values I can solve it for you. Example:

TTS Approver Option 1: Michael Thompson (username: mthompson)

Options 2: Second Name(username: sname)

...

...

Like Michael Thompson likes this
Michael Thompson
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.
Jul 16, 2019

I'm looking at 43 people with full names listed, but as you listed in your Option 1 item, the username format is first letter/last name (Michael Thompson becomes mthompson). this is true for all entries.

You are awesome Alejandro, thank you for your help!

Alejandro Suárez
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.
Jul 16, 2019

Hi @Michael Thompson 

Here you go, it will pick that pattern and set it in the user picker field. Hope it helps :)

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.Query

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

CustomField cfTTSapprover = customFieldManager.getCustomFieldObject(67890L) //Change with the ID of your "TTS Approver" CustomField
CustomField cfNewApprover = customFieldManager.getCustomFieldObject(12345L) //Change with the ID of your "New Aprrover" CustomField

String filter = "\"cf[${cfTTSapprover.getId()}]\" is not EMPTY"
Query query = ComponentAccessor.getComponent(JqlQueryParser).parseQuery(filter)
SearchResults<Issue> search = ComponentAccessor.getComponent(SearchService).search(currentUser, query, new PagerFilter())

Option TTSapprover
String[] split
String userName
String firstNameLetter
String lastName

search?.getResults()?.each {

TTSapprover = it.getCustomFieldValue(cfTTSapprover) as Option
split = TTSapprover.getValue().split(" ")
firstNameLetter = split[0].charAt(0).toLowerCase()
lastName = split[1].toLowerCase()
userName ? ComponentAccessor.getUserManager().getUserByName(firstNameLetter + lastName) : null
cfNewApprover.updateValue(null, it, new ModifiedValue(it.getCustomFieldValue(cfNewApprover), userName), new DefaultIssueChangeHolder())
ComponentAccessor.getComponent(IssueIndexingService).reIndex(it)
}
Michael Thompson
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.
Jul 17, 2019

You are amazing Alejandro, thank you so much!

Like Alejandro Suárez likes this
1 vote
John Funk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jul 15, 2019

Hey Michael,

You might could export as CSV, update the values to match user names and then reimport. 

Michael Thompson
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.
Jul 15, 2019

Thanks John. I had thought of that, but exporting and importing over 35,000 records will likely kill my server, if I can even get Jira to handle that high a number.

John Funk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jul 16, 2019

Yeah, you would probably have to do that 1,000 at a time. So, I guess it becomes, which one is faster - or less painful  :-(

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events