Forums

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

Calculate User Pain scores automatically

Harit Patel April 18, 2020

To help with bug prioritization, would like to automatically calculate a Score with ScriptRunner

There are 4 fields.

  1. Type - 1,2,3,4,5 (select list, single choice)
  2. Priority - 1,2,3,4,5 (select list, single choice)
  3. Impact - 1,2,3,4,5 (select list, single choice)
  4. Score - Number Field

When all three fields (Type, Priority and Impact) are selected, would like to automatically calculate the score based on a calculation.

Here is what I have, not sure where I am struggling.

 

// Get custom fields
def customFields = get("/rest/api/2/field")
.asObject(List)
.body
.findAll { (it as Map).custom } as List<Map>

def input1Field = customFields.find { it.name == 'Type' }?.id
def input2Field = customFields.find { it.name == 'Priority' }?.id
def input3Field = customFields.find { it.name == 'Impact' }?.id
def outputField = customFields.find { it.name == 'Score' }?.id

def input1 = issue.fields[input1Field] as Integer
def input2 = issue.fields[input2Field] as Integer
def input3 = issue.fields[input3Field] as Integer

def output = (input1*input2*input3*100)/125

if (output == (issue.fields[outputField] as Integer)) {
  logger.info("already been updated")
  return
}

put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields:[
(outputField): output
]
])
.asString()

 

2 answers

0 votes
Ravi Sagar _Sparxsys_
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.
April 18, 2020

Hi @Harit Patel 

Quickly looked at the code, didn't quite get it why you are fetching the id of the custom field. May be I missed something. Anyways, try something like this below.

 

def firstField = customFieldManager.getCustomFieldObjectsByName("CUSTOMFIELDNAME") 

def firstFieldValue = issue.getCustomFieldValue(firstField[0]) as Double

Once you get the value then do you usual calculations on those values.

 

Ravi

Harit Patel April 18, 2020

Thanks Ravi, am not a developer, it shows... haha

I changed it to match with my code, did you mean something like this.

def input1Field = customFields.getCustomFieldObjectsByName("Type")
def input1 = issue.getCustomFieldValue(input1Field[0]) as Double
Like Ravi Sagar _Sparxsys_ likes this
Ravi Sagar _Sparxsys_
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.
April 18, 2020

Yes now input1 will have the value of your "Type" field.

Harit Patel April 26, 2020

I was able to fix it by adding .value for that piece of the code.

def input1 = issue.fields[input1Field].value as Integer
def input2 = issue.fields[input2Field].value as Integer
def input3 = issue.fields[input3Field].value as Integer

Here's how the full code looks like. Now I need to use the Score to update the Priority Levels for the JIRA ticket and add a Comment that ScriptRunner made the changes.

// get custom fields
def customFields = get("/rest/api/2/field")
.asObject(List)
.body
.findAll { (it as Map).custom } as List<Map>

def input1CfId = customFields.find { it.name == 'Type' }?.id
def input2CfId = customFields.find { it.name == 'Priority' }?.id
def input3CfId = customFields.find { it.name == 'Impact' }?.id
def outputCfId = customFields.find { it.name == 'Score' }?.id

def input1 = issue.fields[input1CfId].value as Integer
def input2 = issue.fields[input2CfId].value as Integer
def input3 = issue.fields[input3CfId].value as Integer

if (input1 == null || input2 == null || input3 == null) {
logger.info("Calculation using ${input1}, ${input2} and ${input3} is not possible")
return
}

def output = (input1 * input2 * input3 * 100)/125

put("/rest/api/2/issue/${issue.key}")
//.queryString("overrideScreenSecurity", Boolean.TRUE)
.header("Content-Type", "application/json")
.body([
fields: [
(outputCfId): output
]
])
.asString()

 

 

0 votes
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.
April 18, 2020

Suggest an answer

Log in or Sign up to answer