Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Script

Code Block
//// 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.

  • The form will need to be open, or re-opened in order for the script to populate the destination field.

  • 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.

...