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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,556,692
Community Members
 
Community Events
184
Community Groups

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

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.
Feb 12, 2020 • edited

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)

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.
Feb 12, 2020

What error do you get?

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.
Feb 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.

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.
Apr 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