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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,120 views 13 19
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot