Make a scripted field multiply together the values of 3 custom fields

Daniel_Howard February 12, 2020

I have 3 custom fields with the values of say  3, 7 & 8 - I want to stop the need for our 4th field to be manually updated by multiplying together these 3 values.

 

How can I create a scripted field that does the following

CustomFieldA * CustomFieldB * CustomFieldC  = CustomFieldD (the scripted field)

 

 

1 answer

3 votes
Leonard Chew
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.
February 12, 2020

I gather you are using scriptrunner?

If so, this should do the job:

Create the Scripted Field "CustomFieldD" with this script:

import com.atlassian.jira.component.ComponentAccessor

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

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

return (CustomFieldAValue * CustomFieldBValue * CustomFieldCValue)

 

 

Daniel_Howard February 12, 2020

Thanks for the update, so if I insert the fields like so - each of the fields are named below - they will have a value of 1 to 10.. tried this but the number field is not calculating?

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def CustomFieldA = customFieldManager.getCustomFieldObjectsByName("FMEA Severity")[0]
def CustomFieldB = customFieldManager.getCustomFieldObjectsByName("Detectability")[0]
def CustomFieldC = customFieldManager.getCustomFieldObjectsByName("Occurrence")[0]

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

return (CustomFieldAValue * CustomFieldBValue * CustomFieldCValue)

Leonard Chew
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.
February 12, 2020

What error do you get?

Daniel_Howard February 12, 2020

Last logs error is this

 

Time (on server): Wed Feb 12 2020 16:26:16 GMT+0000 (Greenwich Mean Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2020-02-12 16:26:16,379 ERROR [customfield.GroovyCustomField]: *************************************************************************************
Script field failed on issue: FMEA-277, field: RPN
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '10' with class 'com.atlassian.jira.issue.customfields.option.LazyLoadedOption' to class 'float'
 at Script24.run(Script24.groovy:8)
Leonard Chew
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.
February 12, 2020

OK, it has problems with casting the value '10' to float. 

Can you go to the Custom Fields Admin page and check the field types of the fields  'FMEA Severity', 'Detectability' and 'Occurrence'?

The field type should be 'Number Field' for all three fields.

Inayat N April 24, 2023

Hi @Leonard Chew , thanks for the script.  It works when I want to multiply two number custom fields.

Do you know how to make it work when multiplying a number custom field with another scripted field?  I tried this, but it did not return the product.  My case:

  • First field is a custom number field (no problem)
  • Second field is a scripted number field (an existing Scriptrunner calculated field)
  • ScriptedField2 = CustomFieldA * ScriptedField1

However, when I multiply the two, it always returns zero, and I think it is because the script does not like the second field.  Is there a syntax update to recognize the second field?      

Thanks.

Leonard Chew
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.
April 26, 2023

@Inayat N In ScriptField1, use a dedicated Method that returns the correct value (store it with the Script Editor on your Server).

In ScrpitedField2 use the same stored Method to multiply with CustomFieldA.

Alternatively you can copy-paste the code of ScriptedField1 into your ScriptedField2, but you have to be careful when doing changes, as the changes will need to be done in both scripts.

Like Inayat N likes this

Suggest an answer

Log in or Sign up to answer