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,364,623
Community Members
 
Community Events
168
Community Groups

Create a calculated custom field based on the values of other custom fields

Hi guys, 

I need to create a non-editable calculated custom field where the value will be based on the values from two other custom fields (single select list). From what I understand this would need to be done in Scriptrunner in my case (on Server/Data Center and don't have JMCF) however, I'm very new to it so I'm not sure how to format the script and when looking online I don't really find what I need.  

Example: 

Field A:

  • 1
  • 2
  • 3

Field B:

  • 1
  • 2

Calculation: Value in Field A + Value from Field B = Value in Field C. 

Anyone got an idea of how to do this in the best way? 

Thanks in advance, 

 

1 answer

0 votes

With scriptrunner, this assumes the most simple case:

Add a scripted field, selecting a template for it as "numeric" (note you will also need to look at the custom field definition to choose a "searcher" - probably want to use "number range" for that), and add a script like:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

final customFieldNameA = 'Field A'
final customFieldNameB = 'Field B'

def customFieldA = customFieldManager.getCustomFieldObjects(issue).find { it.name == customFieldNameA }
def customFieldB = customFieldManager.getCustomFieldObjects(issue).find { it.name == customFieldNameB }

return issue.getCustomFieldValue(customFieldA) + issue.getCustomFieldValue(customFieldB) 

Note that this assumes fields A and B are numeric - if they are not, you will need to convert their values to numbers.  There's also no error checking - it assumes the fields are always filled.

Hi Nic, 

Thank you for your reply! 

I tried with the above but think I'm missing something as the return give me an "unable to return class Impact" error. 

The name of Field A is Probability and Field B is Impact Value. They are both Select List (single choice) with the numbers 1,2,3 as options. Would these need to be Number Fields for this to work? 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

final customFieldNameA = 'Probability'
final customFieldNameB = 'Impact Value'

def customFieldA = customFieldManager.getCustomFieldObjects(issue).find { it.name == customFieldNameA }
def customFieldB = customFieldManager.getCustomFieldObjects(issue).find { it.name == customFieldNameB }

return issue.getCustomFieldValue(Probablility) + issue.getCustomFieldValue(Impact Value)

Thanks a lot for your assistance! 

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events