To help with bug prioritization, would like to automatically calculate a Score with ScriptRunner
There are 4 fields.
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()
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes now input1 will have the value of your "Type" field.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
There's something very similar at https://library.adaptavist.com/entity/set-priority-using-an-impact-urgent-matrix
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.