Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Question regarding custom scripted field

Sasha Pilote May 4, 2023

Hi All,

 

I'm quite new to Scriptrunner and Groovy and am running into a snag with a custom field. 

This field needs to take values from 2 other custom fields called Impact and probability, with dropdown values of (1, 2, 3, 4,) for each and multiply the values (impact * probability).

I have tried scripting it, but my result is not making sense. max result should be 16, I got 102,272,765! 

Is anyone able to help me identify where the issue is and give pointers on how to fix it?

Any help is greatly appreciated.

 Cheers,

Screenshot 2023-05-04 114031 risk.png

2 answers

2 accepted

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

It is because your Impact and Probability fields are not numbers.  Their contents are options with names that are numbers, and you can't cast an option to a number because the data in the option is not numeric.

Try 

def ImpactFieldAValue = (issue.getCustomFieldValue(Impact).getName() ?: 0) as float

instead.  That assumes your options all have plain numbers as their description text though.

Sasha Pilote May 4, 2023

Thank you @Nic Brough -Adaptavist- 

 

I got it working! I changed my impact and probability custom fields to number fields,

then I changed the return to state

 

return (ImpactFieldAValue * ProbabilityFieldBValue
Now it is calculating the fields properly! 
Thanks for the help all.
1 vote
Answer accepted
Benjamin
Community Champion
May 4, 2023

Hi @Sasha Pilote ,

 

Take a look at your log to see what value you got for log.debug.

 

Here's a post on an approach to multiply custom field.

 

https://community.atlassian.com/t5/Jira-questions/Make-a-scripted-field-multiply-together-the-values-of-3-custom/qaq-p/1299362

 

-Ben

Sasha Pilote May 4, 2023

Hi @Benjamin 

 

Thanks for the reply, I tried matching the script to the method shown In the post you shared:

 

import com.atlassian.jira.component.ComponentAccessor


def riskissueType = ComponentAccessor.issueTypeSchemeManager.getIssueTypesForProject(issue.projectObject).find { it.name == "Risk" }


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def Impact = customFieldManager.getCustomFieldObjectsByName("Impact")[0]
def Probability = customFieldManager.getCustomFieldObjectsByName("Probability")[0]


def ImpactFieldAValue = (issue.getCustomFieldValue(Impact) ?: 0) as float
def ProbabilityFieldBValue = (issue.getCustomFieldValue(Probability) ?: 0) as float

return Impact * Probability
With this i'm getting this error at the return:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '4' with class 'com.atlassian.jira.issue.customfields.option.LazyLoadedOption' to class 'float' at Script55.run(Script55.groovy:12)
also says i will get no value in return. I have an existing issue selected under the preview issue key.  what does it look like I'm missing here?

Suggest an answer

Log in or Sign up to answer