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)
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)
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What error do you get?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Inayat Nahvi 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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.