Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How assign values to drop field options and calculate values?

Shah Baloch
Contributor
March 7, 2023

Hi Community,

I'm trying to assign value to the drop-down field option and then multiply those values. For example, I have three custom fields, two are drop-down fields and each drop has five options and the third one is a number field.

Drop down fieldA has options are:
1. Apple
2. Orange
3. Banana
4. Pear
5. Kiwi

Drop down fieldB has options are:
1. Potato
2. Tomato
3. Onion
4. Capsicum
5. Broccoli

If the user selects "Apple" from the first dropdown field and it should assign value 1 and value 2 to Orange and so on. Same with the second drop-down field if the user selects Potato it should assign the value 1, value 2 to Tomato, and so on. Then it should multiply fieldA's value by FieldB's value and display results in the third field totalValue. I tried the below Behaviour script but it didn't work. Any idea how can I make it work?

 
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def fieldA= getFieldById('customfield_17000')
def fieldB= getFieldById('customfield_17001')
def totalScore = getFieldById('customfield_17002')

if(fieldA.value == 'Apple') {
 def valueOne = 1
 }

if(fieldB.value == 'Onion') {
 def valueTwo = 3
 }

def valueTotal = valueOne * valueTwo
totalScore.setFormValue(valueTotal )

 

Thank you

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave Sheehan
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.
March 7, 2023

Hi

Your script doesn't work because you are defining variables inside a code blog and attempting to use them outside of that blog. def valueOne and def valueTwo 

The following would work:

def valueOne, valueTwo

if
(fieldA.value == 'Apple') {
valueOne = 1
}

if(fieldB.value == 'Onion') {
valueTwo = 3
}
def valueTotal = valueOne * valueTwo

 

But here is a more complete example where you won't have a build a bunch of if blocks.

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def fieldA = getFieldById('customfield_17000')
def fieldB = getFieldById('customfield_17001')
def totalScore = getFieldById('customfield_17002')

if (!fieldA.value || !fieldB.value) {
//if either field is empty, set the score to 0 and exit
totalScore.setFormValue('0')
return
}
def fieldAValues = [
Apple : 1,
Orange: 2,
Banana: 3,
Pear : 4,
Kimi : 5
]
def fieldBValues = [
Potato : 1,
Tomato : 2,
Onion : 3,
Capsicum: 4,
Broccoli: 5,
]

def valueTotal = fieldAValues[fieldA.value] * fieldBValues[fieldB.value]
totalScore.setFormValue(valueTotal)
Shah Baloch
Contributor
March 8, 2023

Hi @Peter-Dave Sheehan I tried the code you provided. It's not calculating numbers to create an issue, however, if I go back and edit the issue then it adds the number. For example, if I create an issue select "Oragne" from fieldA and "Onion" from fieldb and click create it displays 0 from the totalScore field. However, if I click edit and resubmit it'll display the calculated number. Any idea why it's not calculating values on create issue time?

Shah Baloch
Contributor
March 10, 2023

@Peter-Dave Sheehanthe other way it works is if I switch issue types. For example, if the "Story" issue type is selected and I select values from both drop-down fields, then switch the issue type to Task or any other issue then it calculates values on create issue screen otherwise it won't calculate values. Do you know why it's not working properly? Thanks

Peter-Dave Sheehan
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.
March 13, 2023

That is because you placed the script in the wrong place.

The "initializer" script runs once when you open the form or when you change the project or issue type (it has to re-initialize in case the new combination of project/issue type results in a different mapping/behaviour).

While server-side scripts on the individual fields will run each time that field is modified (including when the field is populated from the db when opening the edit screen).

So move the script to BOTH fieldA and fieldB. There should be no script until TotalScore.

Then, whenever either of those fields are changed, then the TotalScore field will be refreshed.

Like Shah Baloch likes this
Shah Baloch
Contributor
March 13, 2023

@Peter-Dave SheehanI see. I added script to both fieldA and fieldB and it works. Earlier I tried fields one by one and Initializer. I didn't know have to add the script to both fields. Thank you so much.

TAGS
AUG Leaders

Atlassian Community Events