Calculated Fields.

Purpose

Hosting

Compatible Tools

Purpose

Hosting

Compatible Tools

Calculated Fields

Use this script to sum the values of number fields on the form and place the result in a destination field.

Cloud

Any scripting tool, such as JMWE, Power Scripts or Scriptrunner, that uses Groovy.

Script

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 //// When using this script in the script console uncomment the next line: def issueKey = 'ISSUE-1' //// When using this script in a transition uncomment the next line: // def issueKey = issue.key String formName = "Form Name" def fieldNames = ["Field One", "Field Two"] def fieldDestination = "Field Destination" Closure getFormIds = { def propertyValue, String name -> def ids = [] for(def form : propertyValue.value.forms){ if(form.name.equals(name)){ ids.add(form.id) } } return ids } Closure<String> getFieldId = { def property, String fieldName -> for (def question : property.value.design.questions){ if(fieldName.equals(question.value.label)){ return question.key } } } Closure<String> getFieldValue = { def property, String fieldName -> for (def answer : property.value.state.answers){ String fieldId = getFieldId(property, fieldName) if(answer.key.equals(fieldId)){ return answer.value.text } } } def proFormaForm = get('/rest/api/3/issue/' + issueKey + '/properties/proforma.forms') .header('Content-Type', 'application/json') .asObject(Map) if(proFormaForm.status == 200){ def forms = getFormIds(proFormaForm.body, formName) for(def formId : forms){ def property = get('/rest/api/3/issue/' + issueKey + '/properties/proforma.forms.i' + formId) .header('Content-Type', 'application/json') .asObject(Map) if(property.status == 200){ if(property.body.value.schemaVersion.equals(8)){ Double result = 0 for(def fieldName : fieldNames) { String fieldValue = getFieldValue(property.body, fieldName) if(fieldValue){ result = result + fieldValue.toDouble() } } def fieldDestinationId = getFieldId(property.body, fieldDestination).toString() boolean answerExists = false for(def answer : property.body.value.state.answers){ if(answer.key.equals(fieldDestinationId)){ answer.value.text = result.toString() answerExists = true } } if(!answerExists){ property.body.value.state.answers.put(fieldDestinationId.toString(), ["text" : result.toString()]) } def insertProperty = put('/rest/api/3/issue/' + issueKey + '/properties/' + property.body.key) .header('Content-Type', 'application/json') .body(property.body.value) .asString() } } } }

Installation instructions

  • If used in a transition:

    • Comment line 2

    • Uncomment line 4

    • Change formName in line 6 to the name of the form that has all fields used in the scripts

    • Change/Add in fieldNames in line 7 the name/label of all number fields you want to sum up, separated by comma, between quotation marks.

    • Change fieldDestination in line 8 to the name/label of the destination field that will receive the final sum of all values.

  • If used in the script console:

    • Change the issue key in line 2 to the key of the issue you want to sum the field values in a form.

    • Change formName in line 6 to the name of the form that has all fields used in the scripts

    • Change/Add in fieldNames in line 7 the name/label of all number fields you want to sum up, separated by comma, between quotation marks.

    • Change fieldDestination in line 8 to the name/label of the destination field that will receive the final sum of all values.

Possible use cases

Calculations can be particularly useful when used with financial forms such as purchase orders and expense claims.

Limitations

  • Will override the value in the destination field with the sum of all fields set in the fieldNames variable.

  • Only works with number fields.

  • The script will run on all copies of the indicated form on the issue.

  • The destination field and all fields that will be summed must be on the same form, and on the same issue.

  • Note that this script will not work if the form is over 32 KB. You can limit the size of your forms by using multiple smaller forms, and by including character/word limits on your text fields.