Forums

Articles
Create
cancel
Showing results forĀ 
Search instead forĀ 
Did you mean:Ā 
  • Community
  • Q&A
  • Jira
  • Questions
  • Is it possible to calculate 2 field values with the behaviour plugin & output them in another field?

Is it possible to calculate 2 field values with the behaviour plugin & output them in another field?

Thomas Vollbrecht
May 22, 2019

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def dc = getFieldById("customfield_17120")
def dv = getFieldById("customfield_17109")
def cl = getFieldById("customfield_17110")
def dcval = dc.getValue()
def dvval = dv.getValue()
def clval = cl.getValue()
def Sum = dvval + dcval; //calculate

if (dvval != "" && clval != "" ) {dc.setFormValue(Sum)}

1 answer

1 accepted

0 votes
Answer accepted
Antoine Berry
Community Champion
May 24, 2019

Hi @Thomas Vollbrecht ,

This is possible. You might need to adapt to the custom field types though. For example, if they are number fields, you would need to do a few checks : 

def dc = getFieldById("customfield_17120")
def dv = getFieldById("customfield_17109")
def cl = getFieldById("customfield_17110")
def dcval = dc.getValue()
def dvval = dv.getValue()
if (dvval.isNumber() && dcval.isNumber()){
def Sum = Double.parseDouble(dvval) + Double.parseDouble(dcval)
cl.setFormValue(Sum)
}

If they are text fields, it is straight forward.

Antoine

Thomas Vollbrecht
May 24, 2019

The fields are number fields, but what for checks i need to do?

I test your written code, but it doesn“t work.

 

Could you please send me a instruction or something like that?

Antoine Berry
Community Champion
May 24, 2019

Could you please detail what you need more precisely ? Then I will be able to provide the steps.

Behaviours will detect any change to the mapped field and execute the script each time.

Thomas Vollbrecht
May 24, 2019

Fields:

Field01 (in €)     Number field

Field02 (in €)     Number field

Field03 (in%)     Number field

Calculate: FieldID01 /  FeldID02 * 100

Thanks for your help!

Antoine Berry
Community Champion
May 24, 2019

So this would be the whole script. Update the custom field ids.

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

@BaseScript FieldBehaviours fieldBehaviours

int numberField1Id = 13301
def numberField1 = getFieldById("customfield_" + numberField1Id)
def numberField1Value = numberField1.getValue()

int numberField2Id = 13302
def numberField2 = getFieldById("customfield_" + numberField2Id)
def numberField2Value = numberField2.getValue()

int numberField3Id = 13303
def numberField3 = getFieldById("customfield_" + numberField3Id)

if (numberField1Value.isNumber() && numberField2Value.isNumber()) {
def result = (Double.parseDouble(numberField1Value) / Double.parseDouble(numberField2Value)) * 100
numberField3.setFormValue(result.trunc(2))
}
numberField3.setReadOnly(true)

Remove the setReadOnly if you want your users to be able to edit the field.

You need to map this script to the Field01 and Field02 behaviours.

Antoine

Like • Thomas Vollbrecht likes this
Thomas Vollbrecht
May 24, 2019

Thank you so much!

it works perfectly!

Like • Antoine Berry likes this

Suggest an answer

Log in or Sign up to answer