Calculated Fields.
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 | //// 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 |
|
Possible use cases | Calculations can be particularly useful when used with financial forms such as purchase orders and expense claims. |
Limitations |
|