2 field values multiply Edited

1.uesing script runner

2.create a Script Fields:"cost"

3.Custom 2 fields:number(num type),price(num)

how i can get the cost value using groovy script, once we have updated the filed values pirce, number then immediately calculate the field value cost.

this is my scprit:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def price = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("price"))
def num = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("num")
if (price) {
return price * num)
}
else {
return null
}
erro.png

 

1 answer

2 votes

The field values you are pulling back are objects, not simple numbers.  You need to convert the contents to numbers before you can multiply them together (and bear in mind they might be nulls too, which you'll want to capture and handle)

but price and numbers fields is number type, still need convert???

Look at the error message you showed in your original before you changed the question - the error was saying that the calls to get the field data had returned an object, not a number.

So yes, you need to convert the objects returned into numbers.  (Trying to multiply objects tends not work - yes the  objects you get from the fields might be "4" and "5", but they might also be "banana" and "apple", and there's no way to know what you might want from multiplying those together)

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 25, 2018 in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

465 views 7 5
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you