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
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.