The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

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

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

Rupa Goyal
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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

0 votes
Nic Brough -Adaptavist-
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 Champions.
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