Forums

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

Scriptrunner Script Field - using variables with math functions

Larry Lowe September 12, 2019

I am creating a custom script field using Scriptrunner.

For some reason, when i try to use a variable in a math function (i.e. var1 + var2) it only returns null. If i put in standard numbers (i.e. 9 + 20) then it returns the value.

Here is the code so far, but i plan to add more custom fields once i get this working:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager
def cfAlignmentScore = customFieldManager.getCustomFieldObjectByName("Infra Alignment Score")

def alignment = issue.getCustomFieldValue(cfAlignmentScore) as int

if (alignment != null) {
return (alignment * 5)
}
else {
return null
}

 

1 answer

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 Leaders.
September 12, 2019

As a coder, you do not actually know what is in the custom field.  Trying to cast it to an int automatically might work if the type of data is compatible, but my guess is that it is not.

Try dropping the "as int" on the "def alignment" line first, and then check what the logs say as well (they will usually catch script errors with something more useful than my guesses)

Larry Lowe September 13, 2019

After removing the "as int", i get the attached error. The custom field type is a single select list.

Capture.PNG

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 Leaders.
September 15, 2019

Ok, that suggests that the field does not contain a number at all.  You'll need to work out some code to convert it to one before you can multiply it with another number

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events