Does ScriptRunner allow for this type of calculated field? (see example below)

Neil Goodwin
Contributor
January 31, 2024

I am looking for a addon (or OOTB Jira functionality, but do not believe this is supported) that can handle this type of calculated field. 

Will Scriptrunner be able to support this type of formular on a calculated field?

The single select fields referenced below have numbers associated with them for the calculations.

(Number field1 + Single Select field1 + Single Select field2 x Number field2 x Single Select field3 x Single Select field4) = Calculated field Value)

 

 

2 answers

2 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 31, 2024

Hi @Neil Goodwin 

Script Runner is indeed capable of implementing complex calculated fields like the one you've described. It can execute custom scripts that can read from various fields within an issue, perform calculations, and then use the result as a custom field value. But you have to convert select option values to numbers. 

Below you can find the code snippet as a start.

 

def numberField1 = customFieldManager.getCustomFieldObjectByName("Number field1").getValue(issue)
def singleSelectField1Value = customFieldManager.getCustomFieldObjectByName("Single Select field1").getValue(issue)?.getOptionId()
def singleSelectField2Value = customFieldManager.getCustomFieldObjectByName("Single Select field2").getValue(issue)?.getOptionId()
def numberField2 = customFieldManager.getCustomFieldObjectByName("Number field2").getValue(issue)
def singleSelectField3Value = customFieldManager.getCustomFieldObjectByName("Single Select field3").getValue(issue)?.getOptionId()
def singleSelectField4Value = customFieldManager.getCustomFieldObjectByName("Single Select field4").getValue(issue)?.getOptionId()

// convert option value to number
def singleSelectField1Number = getNumberForOption(singleSelectField1Value)
def singleSelectField2Number = getNumberForOption(singleSelectField2Value)
def singleSelectField3Number = getNumberForOption(singleSelectField3Value)
def singleSelectField4Number = getNumberForOption(singleSelectField4Value)

return (numberField1 + singleSelectField1Number + singleSelectField2Number) * numberField2 * singleSelectField3Number * singleSelectField4Number

// Placeholder function for getting the number associated with a single select field option
def getNumberForOption(Long optionId) {
....
return ...
}

Assume that getNumberForOption method is the method for converting the option values to numbers.

 

I hope that helps!

Humberto Gomes
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.
January 31, 2024

Hi, can get this using automation, do not need a script runner for this.

see

Jira smart values - math expressions | Cloud automation Cloud | Atlassian Support

it works also for DC version.

 

Let me know if you need additional support.

Like Neil Goodwin likes this
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.
January 31, 2024

Except that you can't convert the select lists to numbers, you would have to write potentially hundreds of "if" statements.

Like # people like this
0 votes
Answer accepted
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.
January 31, 2024

Welcome to the Atlassian Community!

If you have a more recent version of Scriptrunner, then you can do this much more easily by using HAPI.

You have not told us exactly how your select lists are representing numbers in their options, so I've made an assumption, but:

def issueKey = "ISSUE-KEY" // Replace with the actual issue key - this is written as a console script, where you have to feed it the issue you want to work with

def issue = Issues.getByKey(issueKey)

// Retrieve the field values
def numberFieldValue1 = issue.getCustomFieldValue('Numberfield1') as Double // Assuming the field contains a numeric value
def singleSelectFieldValue1 = issue.getCustomFieldValue('SingleSelectfield1').value as Double // Assuming the option has a numeric string value
def singleSelectFieldValue2 = issue.getCustomFieldValue('SingleSelectfield2').value as Double 
def numberFieldValue2 = issue.getCustomFieldValue('Numberfield2') as Double 
def singleSelectFieldValue3 = issue.getCustomFieldValue('SingleSelectfield3').value as Double 
def singleSelectFieldValue4 = issue.getCustomFieldValue('SingleSelectfield4').value as Double 

def result = (numberFieldValue1 + singleSelectFieldValue1 + (singleSelectFieldValue2 * numberFieldValue2 * singleSelectFieldValue3 * singleSelectFieldValue4))

log.debug("The result of the calculation is: $result")

// If you need to store the result back into another custom field, you can do so like this:
issue.update {
    setCustomFieldValue('ResultField', result.toString()) /

 

 

Neil Goodwin
Contributor
February 1, 2024

Thank you all for the replies and the +1 of providing some scripts.

The dropdowns have text that are associated with values (screenshot below)  It sounds like ScriptRunner is the way to go, as the OOTB functionality may/will require too many "if" statements.

image.png

TAGS
AUG Leaders

Atlassian Community Events