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,552,168
Community Members
 
Community Events
184
Community Groups

Question regarding custom scripted field

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-
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 04, 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.

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 Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 04, 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

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