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

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

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.
July 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

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
July 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 - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
July 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.
July 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 - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
July 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.
July 17, 2019

You are amazing Alejandro, thank you so much!

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.
July 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.
July 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.
July 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  :-(

TAGS
AUG Leaders

Atlassian Community Events