Forums

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

Calculation using Jira Scriptlistener for Cloud

Ash
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.
August 19, 2019

Hi All,

 

I'm trying to calculate 3 (single select drop down fields) and populate in a number field.

Here is the sample script, what i have tried. can anyone help me with the snippet for score calculation.

Busniess Priority - Single select drop option with values (1,2,3)

Severity - Single select drop option with values (1,2,3)

Mandatory - Single select drop option with values (1,2)

Score - output field (field type number)

 

def customFields = get("/rest/api/2/field")
.header('Content-Type', 'application/json')
.asObject(Map)
.body

def fields = issue.fields as Map
//def output

// Get the Custom field to get the option value from
def customField1 = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == 'Busniess Priority'
} as Map

def customField2 = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == 'Severity'
} as Map

def customField3 = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == 'Mandatory'
} as Map

// Extract and store the option from the custom field
def value1 = (fields[customField1.id] as Map).value
def value2 = (fields[customField2.id] as Map).value
def value3 = (fields[customField3.id] as Map).value

def outputCfId

def output = value1 * value2 * value3

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

 

TIA!

3 answers

1 accepted

1 vote
Answer accepted
Ash
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.
August 19, 2019

Found the solution finally,

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


def custField1 = customFields.find { it.name == 'Business Priority'}?.id
def custField2 = customFields.find { it.name == 'Severity'}?.id
def custField3 = customFields.find { it.name == 'Mandatory'}?.id
def outputCfId = customFields.find { it.name == 'Score' }?.id
def projectKey = "ASH"

if (issue == null || issue.fields.project.key != projectKey) { (1)
logger.info("Wrong Project ${issue.fields.project.key}")
return
}

def value1 = (issue.fields[custField1] as Map).value as Integer
def value2 = (issue.fields[custField2] as Map).value as Integer
def value3 = (issue.fields[custField3] as Map).value as Integer

if (value1 == null || value2 == null || value3 == null) {
logger.info("Calculation using ${value1} and ${value2} and ${value3} was not possible")
return
}

def output = value1 * value2 * value3

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

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

Ash
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.
August 20, 2019

How to override Score value if output is null. 

0 votes
Ash
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.
August 19, 2019

Here is the latest code with the error


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


def custField1 = customFields.find { it.name == 'Business Priority'}?.id
def custField2 = customFields.find { it.name == 'Severity'}?.id
def custField3 = customFields.find { it.name == 'Mandatory'}?.id
def outputCfId = customFields.find { it.name == 'Score' }?.id
def projectKey = "ASH"

if (issue == null || issue.fields.project.key != projectKey) { (1)
logger.info("Wrong Project ${issue.fields.project.key}")
return
}

def value1 = issue.fields[custField1] as Integer
def value2 = issue.fields[custField2] as Integer
def value3 = issue.fields[custField3] as Integer

if (value1 == null || value2 == null || value3 == null) {
logger.info("Calculation using ${value1} and ${value2} and ${value3} was not possible")
return
}

def output = value1 * value2 * value3

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

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

 

 

Error:

2019-08-19 19:26:37.914 INFO - Serializing object into 'interface java.util.List'
2019-08-19 19:26:38.736 INFO - GET /rest/api/2/field asObject Request Duration: 3680ms
2019-08-19 19:26:39.092 ERROR - Cannot coerce a map to class java.lang.Integer because it is a final class on line 19
2019-08-19 19:26:39.156 ERROR - Class: com.adaptavist.sr.cloud.events.WebhookExecution, Config: null
0 votes
Brittany Wispell
Community Champion
August 19, 2019

Hi @Ash 

Have you taken a look at this documentation. This is currently the closest you can get with a field calculation. 

Calculate Custom Field on Issue Update

Ash
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.
August 19, 2019

Hi @Brittany Wispell , thanks for your response. 

I tried this as well, i received below error while using multiplication in the same code.

No signature of method: java.lang.String.multiply() is applicable for argument types: (java.lang.String) values:

Suggest an answer

Log in or Sign up to answer