customfields.option.lazyloadedoption to class int

Yuval Maron January 10, 2021

Hey I'm using scriptRunner,

I'm trying to set a textfield value based on calculation of two selected values from two different list. (post function)

For example: list1[1, 2, 3] list 2[4,5,6] textfield val= list1val*list2val

The main issue is i can't convert the selected values into integer in order to do the calculation (i'm converting it to int using as int) and  in the post function area the error says: customfields.option.lazyloadedoption to class int.

Thanks in advance

2 answers

0 votes
Radek Dostál
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.
January 10, 2021

As Nic says - Option is a totally different class from Integer.

 

If your values are always numerical then you would first need to get the value itself, and convert that, not the whole Option.

 

So if you do

def cfValue = issue.getCustomFieldValue(selectField)

This should return a LazyLoadedOption to my best-shabby-and-totally-unreliable-memory.

From this LazyLoadedOption, you now need to get the actual String value (the option name) - https://docs.atlassian.com/software/jira/docs/api/8.5.8/com/atlassian/jira/issue/customfields/option/LazyLoadedOption.html#getValue-- (first bookmarked javadoc, it will be the same with Cloud anyway though), so

String optionName = cfValue.getValue()

 

Then, assuming this is a valid number, you can cast it to an Integer:

Integer.valueOf(optionName)

or the Groovy way

def optionName = cfValue.getValue()
or
int optionName = cfValue.getValue() as int

 

If you're using dynamic classes like 'def' then Groovy should be able to do Math operations on those values if they are numerical, because Groovy just identifies if they are compatible types.

The main difference is not to do this operation with the LazyLoadedOption, you need to get the "value" first.

 

With Cloud though I do not know if you are handling this through Java API directly or if you need to fetch json with REST -- I don't automate Cloud so I don't know the options there too well, my suggestion above would apply to Server.

However, this should hopefully get you in the right way regardless of using Java or REST.

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.
January 10, 2021

You can't just cast an object to another type if the objects are totally different. 

An option is not a number and has nothing in common with a number.

You will need to code rules that read the option and set a number, or if your select list really will only have numbers as their option names, then you could use the parseint function to convert. 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events