Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
Community showcase
Published in Apps & Integrations

Send an Email or Publish to Confluence - What should you do with your release notes?

Background When you hear the words ‘Release notes’, almost always you think of an unsolicited email from a software vendor. But I am here to tell you that from our data, sending release notes via E...

37 views 0 1
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you