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

Multiply 2 numerical custom fields and display result in third custom field

Adrian_Avalos May 12, 2023

Looking for a script that can be used to display the numerical outcome of two numerical custom fields in scriptrunner. Preferably behavuiour, but am open to adding in listener, custom scriptrunner field, or post function. Will need specifics as I'm very new to scriptrunner.

Custom Field A*Custom Field B=Custom Field C

3 answers

1 accepted

1 vote
Answer accepted
Adrian_Avalos May 15, 2023

Thank you all. I found a solution via creating a custom field in scriptrunner. Below is what I used

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def CustomFieldA = customFieldManager.getCustomFieldObjectsByName("CFA")[0]
def CustomFieldB = customFieldManager.getCustomFieldObjectsByName("CFB")[0]

def CustomFieldAValue = (issue.getCustomFieldValue(CustomFieldA) ?: 0) as float
def CustomFieldBValue = (issue.getCustomFieldValue(CustomFieldB) ?: 0) as float

return (CustomFieldAValue * CustomFieldBValue)
2 votes
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 13, 2023

Hi @Adrian_Avalos and welcome,

you could use a calculated custom field provided by Script Runner and use the following code :

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def aCF = customFieldManager.getCustomFieldObjectByName("Custom Field Name A")

def bCF = customFieldManager.getCustomFieldObjectByName("Custom Field Name B")

def aS = issue.getCustomFieldValue(aCF) as String

def bS = issue.getCustomFieldValue(bCF ) as String

def a = aS as Double

def b = bS as Double

if (a == null || b == null) return null

return (a * b) as Double

Please try it and let me know if it works.

Fabio

1 vote
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.
May 13, 2023

I would use a version of Scriptrunner with HAPI enabled:

def issueKey = "YOUR_ISSUE_KEY"
def firstNumberField = "First Number Field"
def secondNumberField = "Second Number Field"
def resultField = "Result Field"

def issue = Issues.getByKey(issueKey)

def firstNumber = issue.getCustomFieldValue(firstNumberField) as Double
def secondNumber = issue.getCustomFieldValue(secondNumberField) as Double
def result = firstNumber * secondNumber

issue.update {
    setCustomFieldValue(resultField, result)
}

This would work as a console script, but you would remove "issueKey" for a post-function or listener, and if you did it as a scripted field, it gets even shorter, as you drop the "issue update" bit, and instead of "Def result = ", use "return "

I'd avoid a Behaviour for this, because that means having fields on screen that people could then amend later, and won't work if people make updates or edits with REST calls or apps that don't use the API.

Suggest an answer

Log in or Sign up to answer