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,457,049
Community Members
 
Community Events
176
Community Groups

Create a calculated number scripted field in Scriptrunner based on values from a single select field

I need to calculate a score (number) based on string values selected from single select fields.

I'm not a great groovy scriptwriter so I'm sure my approach on how to define the numeric values based on the string option select is wrong. Please let me know how I could accomplish this. Thanks!

 

import com.atlassian.jira.component.ComponentAccessor;
def customFieldManager = ComponentAccessor.customFieldManager


//define custom fields

def cfA = customFieldManager.getCustomFieldObject(26918) // custom field name "PII"
def cfB = customFieldManager.getCustomFieldObject(26914) // custom field name "Signal"
def cfC = customFieldManager.getCustomFieldObject(26915) // custom field name "Auth"
def cfD = customFieldManager.getCustomFieldObject(26916) // custom field name "Public-Facing?"
def cfE = customFieldManager.getCustomFieldObject(26917) // custom field name "Cryptography"


//get custom field values

def valA = cfA.getValue(issue)
def valB = cfB.getValue(issue)
def valC = cfC.getValue(issue)
def valD = cfD.getValue(issue)
def valE = cfE.getValue(issue)


//give select option numeric values

def scoreA = {
if (valA() == 'Yes') 5 as Double;
else 0 as Double;
}

def scoreB = {
if (valB() == 'Yes') 5 as Double;
else 0 as Double;
}

def scoreC = {
if (valC() == 'Internal') 5 as Double;
else if (valC() == 'External') 8 as Double;
else 0 as Double;
}

def scoreD = {
if (valD() == 'Yes (whole internet)') 5 as Double;
else if (valC() == 'Yes (only to some IPs)') 3 as Double;
else 0 as Double;
}

def scoreE = {
if (valE() == 'Yes') 5 as Double;
else 0 as Double;
}


//add numbers together for priority calculation value

return (scoreA + scoreB + scoreC + scoreD + scoreE) as Double

 

 

 

1 answer

1 accepted

0 votes
Answer accepted

Hi @chris horton

For your requirement, you could try something like:-

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager

def sampleList = customFieldManager.getCustomFieldObjectsByName('Sample List').first()
def typeList = customFieldManager.getCustomFieldObjectsByName('Type List').first()

def sampleListValue = issue.getCustomFieldValue(sampleList)
def typeListValue = issue.getCustomFieldValue(typeList)

def sampleListValueMap = ['Option1':25, 'Option2':50, 'Option3':75, 'Option4':100] as Map<String, Long>

def typeListValueMap = ['Type1':10, 'Type2':20, 'Type3':30, 'Type4':40] as Map<String, Long>

if (sampleListValue && typeListValue) {
def option1 = sampleListValueMap[sampleListValue.toString()] as Long
def option2 = typeListValueMap[typeListValue.toString()] as Long
(option1 + option2) as Long
} else {
0
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to modify it accordingly.

Below is a screenshot of the Scripted Field configuration:-

scripted_field_configuration.png

Below are a couple of test screenshots.

1. Firstly, when the issue is created, the Sample List is set to Option1, and the Type List is set to Type1 as shown in the screenshot below:-

test1.png

2. Once the issue is created, the Total field is displayed with the value of 35, i.e. the value set for Option1 in the sampleListValueMap is 25, and the value for the Type1 in the typeListValueMap is 10. The total of the two values is 35, as expected, as shown in the screenshot below:-

test2.png

 

I hope this helps to answer your question. :-)

Thank you and Kind regards,
Ram 

Suggest an answer

Log in or Sign up to answer