Copy Select List field (single select) to a number field in Jira

Rupa Jain April 16, 2020

Hi,

I am using Jira Software version 8.6 with Scriprunner.

Trying to write a listener that reads a single select list custom field and copies it to a number custom field of an issue.

I am getting the following error:
Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '13' with class 'com.atlassian.jira.issue.customfields.option.LazyLoadedOption' to class 'java.lang.Double'

I am using the following code:

"New Story Points" - Single Select list

"Story Points" - Number field

import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

def newStoryPointsChanged = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "New Story Points"}

if (newStoryPointsChanged){
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def targetField = customFieldManager.getCustomFieldObjectByName("Story Points")
def sourceField = customFieldManager.getCustomFieldObjectByName("New Story Points")

def sourceFieldValue = (Double) event.issue.getCustomFieldValue(sourceField)
def targetFieldValue = event.issue.getCustomFieldValue(targetField)

targetField.updateValue(null, event.issue, new ModifiedValue(targetFieldValue, sourceFieldValue),new DefaultIssueChangeHolder())
}

Can you suggest the correct way for the following line:

targetField.updateValue(null, event.issue, new ModifiedValue(targetFieldValue, sourceFieldValue),new DefaultIssueChangeHolder())

Thanks,

Rupa

1 answer

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 19, 2020

Select lists hold an "option" object, not a number, so your (Double) cast is nonsense.  It's a like asking something like "what number is my penguin?".

My guess is that your "New Story Points" holds options with names that are displayed to the user like "one, two, three..." or "1, 2, 3...".  You'll need to convert those labels into numbers.  So

def sourceFieldValueName = event.issue.getCustomFieldValue(sourceField)
def saveValue = 0
if ("one".equals(sourceFieldValueName) ) {saveValue = 1}
if ("two".equals(sourceFieldValueName) ) {saveValue = 2}

or perhaps use "parseInt" to convert the name string to a number

Suggest an answer

Log in or Sign up to answer