Calculate Custom Field on Issue Update

Overview

Currently, you cannot add a calculated custom field in Jira Cloud. However, using the IssueUpdated event, the sum of multiple fields can be calculated when an issue is updated.

Example

My project has three custom fields: Cost, Shipping Cost, and Total Cost. These fields are available for all issue types. When an issue or sub-task is created, I can define the values of the Cost and Shipping Cost fields, however, the Total Cost field is not automatically calculated as calculated custom fields are not available in Jira Cloud. Using this script I can trigger a calculation when the issue is updated, allowing me to sum the Cost and Shipping Cost the Total Cost field.

Good to Know

  • The code is designed to be used with the Issue Updated event.
  • If using the add-on user to run the script, it is possible to set the overrideScreenSecurity property to modify fields that are not on the current screen.

Requirements

Jira Jira

    
final projectKey = "TEST" // get custom fields def customFields = get("/rest/api/2/field") .asObject(List) .body .findAll { (it as Map).custom } as List def input1CfId = customFields.find { it.name == 'Custom Field 1' }?.id def input2CfId = customFields.find { it.name == 'Custom Field 2' }?.id def outputCfId = customFields.find { it.name == 'Output Custom Field' }?.id def fields = issue.fields as Map def project = fields.project as Map if (issue == null || project.key != projectKey) { logger.info("Wrong Project ${project.key}") return } def input1 = fields[input1CfId] as Integer def input2 = fields[input2CfId] as Integer if (input1 == null || input2 == null) { logger.info("Calculation using ${input1} and ${input2} was not possible") return } def output = input1 + input2 if (output == (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()
Discovered an issue? Report it here

Suggested for you