Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Need script on workflow postfunction, to calculate the formula

U Sravani September 24, 2019

field A = Custom number field,

field B =Custom number field

Want to update field C= (A-B)/B on a workflow transition.

 

Can someone help on this?

2 answers

1 accepted

0 votes
Answer accepted
Ilya Turov
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.
September 24, 2019

something like this (if field A and B are required somewhere before, you can remove the if block) :

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager
def customFieldA = customFieldManager.getCustomFieldObjectsByName("field A")[0]
def customFieldB = customFieldManager.getCustomFieldObjectsByName("field B")[0]
def customFieldC = customFieldManager.getCustomFieldObjectsByName("field C")[0]

def valueA = issue.getCustomFieldValue(customFieldA)
def valueB = issue.getCustomFieldValue(customFieldB)

if (!valueA || !valueB) {
return
}

def valueC = (valueA - valueB) / valueB

issue.setCustomFieldValue(customFieldC, valueC) 
U Sravani September 24, 2019

Thanks A lot...….!

 

This is working for me.

1 vote
Nic Brough -Adaptavist-
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.
September 24, 2019

There are several examples of calculations in ScriptRunner at https://library.adaptavist.com/search?q=calculate 

I would also think about using a scripted field to do it - this would mean the field would always be updated and valid without you needing to do a calculation on every post-function and write an additional listener to catch "field B or A have changed during an edit operation".

Ilya Turov
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.
September 24, 2019

wish I found out about this library sooner

Nic Brough -Adaptavist-
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.
September 24, 2019

We've had it for a while (maybe a year?  I can't remember exactly) and when it started out, we only had a few scripts.  It's been steadily growing, and I mention it more and more because it's now usually got something that's a good starting point for a lot of more complex scripts!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events