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

chris horton November 28, 2022

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
Ram Kumar Aravindakshan _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.
November 29, 2022

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 

chris horton November 29, 2022

Thanks @Ram Kumar Aravindakshan _Adaptavist_ !! That's what I needed!

Suggest an answer

Log in or Sign up to answer