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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.