2 field values multiply

john 7 April 8, 2017

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
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.
April 8, 2017

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)

john 7 April 8, 2017

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

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.
April 9, 2017

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