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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,460,039
Community Members
 
Community Events
176
Community Groups

How to calculate % based on 2 custom field values (Scriptrunner)

Edited

Hello, 

 

We have a requirement to have a scripted field automatically calculate % value in real time, based on Actual and Target number fields.

We have scriptrunner installed, so we have to use that tool.

If anyone has a sample Groovy code for that it will be greatly appreciated!

Use Case: 

1. CF [1] - Target number
2. CF [2] - Actual number

3. Scripted field = CF[2]/CF[1] - always displayed and updates in real time if CF[1][2] chnaged.

 

Thanks

1 answer

1 accepted

3 votes
Answer accepted
Hana Kučerová Community Leader Aug 06, 2021

Hi @Alex Kulichkov ,

please try something like this (just replace the constants with your custom field ids):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

Long CUSTOM_FIELD_ID_1 = 10359L
Long CUSTOM_FIELD_ID_2 = 10400L

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField customField1 = customFieldManager.getCustomFieldObject(CUSTOM_FIELD_ID_1)
CustomField customField2 = customFieldManager.getCustomFieldObject(CUSTOM_FIELD_ID_2)

Float number1 = issue.getCustomFieldValue(customField1) as Float
Float number2 = issue.getCustomFieldValue(customField2) as Float

return (number1 && number2) ? number2 / number1 * 100 : 0

Hi @Hana Kučerová,

 

Thank you very much, it worked perfectly! Great script example for all simple % based calculations.

Like Hana Kučerová likes this

@Hana Kučerová Thank you so much for this. It helped a lot! 

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events