Forums

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

How to select an option of a Select List (single choice) based on the value of a text custom field

Almu January 19, 2022

Hi guys,

I have this code in the Scriptrunner console, but it does not select the option in cf1 taken into account the value of cf. 

I cannot use the built-in scriptrunner due to the different data type of these custom fields.

Here I have 1 issue, but I need to do that for 8000 issues.

Please, can you help me? Thanks a lot! 

..............................................................

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption


def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager

def issue = ComponentAccessor.getIssueManager().getIssueObject('JIRA-20343')

//Text scripted field "Max Risk Level Old"
def cf = customFieldManager.getCustomFieldObjectsByName("Max Risk Level Old")
def value = issue.getCustomFieldValue(cf)
//return value

//Select List (single option) field "Max Risk Level Old"
def cf1 = customFieldManager.getCustomFieldObjectsByName("Max Risk Level")[0]
def fieldConfig1 = cf1.getRelevantConfig(issue)
def options1 = optionsManager.getOptions(fieldConfig1)
//return options1
//def value1 = issue.getCustomFieldValue(cf1)
//return value1

issue.setCustomFieldValue(cf1, value)

3 answers

1 accepted

1 vote
Answer accepted
Almu January 19, 2022

Finally I get it :-)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.event.type.EventDispatchOption

def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def issue = ComponentAccessor.getIssueManager().getIssueObject('JIRA-20343')

//Text scripted field "Max Risk Level Old"
def cf = customFieldManager.getCustomFieldObjectByName("Max Risk Level Old")
def value = issue.getCustomFieldValue(cf)
//return value

//Select List (single option) field "Max Risk Level Old"
def cf1 = customFieldManager.getCustomFieldObjectByName("Max Risk Level")
def fieldConfig1 = cf1.getRelevantConfig(issue)
def options1 = optionsManager.getOptions(fieldConfig1)
//return options1

for (option in options1) {
if(option.getValue() == value) {
issue.setCustomFieldValue(cf1, option)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
}
}

0 votes
Almu January 19, 2022

Hi there

I have modified the code this way

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.event.type.EventDispatchOption

def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def issue = ComponentAccessor.getIssueManager().getIssueObject('JIRA-20343')

//Text scripted field "Max Risk Level Old"
def cf = customFieldManager.getCustomFieldObjectByName("Max Risk Level Old")
def value = issue.getCustomFieldValue(cf)


//Select List (single option) field "Max Risk Level Old"
def cf1 = customFieldManager.getCustomFieldObjectByName("Max Risk Level")

issue.setCustomFieldValue(cf1, value)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

 

And I get this same error that In "Built-in scripts -> Copy custom field values" section:

java.lang.ClassCastException: java.lang.String cannot be cast to com.atlassian.jira.issue.customfields.option.Option

 

At this point I wonder:

Do you know how to convert a String, which is the cf.value, into an Option, which is the cf1.values?

Thanks!

0 votes
Tansu Akdeniz
Community Champion
January 19, 2022

Hi @Almu 

As I understood, you would like to copy value from scripted field (old one) to single select (new one) for 8000 issues, right? 

In "Built-in scripts -> Copy custom field values" section, you can define the filter (to define issues to be used), source field and target field. As I know, scripted field is supported there. You can try for one single issue at first.

Almu January 19, 2022

Hi Tansu,

Thanks a lot for your swift reply. 

I tried and I get this error

2022-01-19 11:56 /rest/scriptrunner/latest/canned/com.onresolve.scriptrunner.canned.jira.admin.CopyCustomField [c.o.s.c.jira.admin.CopyCustomField] java.lang.ClassCastException: java.lang.String cannot be cast to com.atlassian.jira.issue.customfields.option.Option
Source custom field type: Scripted Field
Target custom field type: Select List (single choice)

 

Any idea, please? Thanks!

Tansu Akdeniz
Community Champion
January 19, 2022

Not at all @Almu 

It seems that data type difference issue occurs. Maybe you can take a look at automation for jira add-on if you have.

Also, if there are not many different option values in scripted field, you can group them via jql filters and run a bulk change.

It is also possible to do it via script in console. But you should go further to run for given jql within the code. Sample script.

Suggest an answer

Log in or Sign up to answer