You can now vote for scripts to help people know if they're useful or not. Login or create an account to vote!

2

Calculate Total Value of Selected Checkboxes

Overview

Calculate the total value of selected checkboxes when the user has selected at least one option from a checkbox field. A new field is auto-populated after the user has selected at least one option from the checkbox.

Example

As a Finance manager, I want to know the total cost of the employee's expenses. When they select expenses options from the checkbox menu, I want those values added together and displayed in a Total Cost field.

Good to Know

The example below is using two types of fields. One checkbox field is the list of expenses, and the other one is a single line custom field called Total Cost which will be auto-populated.

Requirements

  • Jira Jira (8.0 - 8.14)
  • ScriptRunner for Jira ScriptRunner for Jira (6.18.0)
    
import org.apache.log4j.Level log.setLevel(Level.DEBUG) def textField = getFieldByName('Total Expenses') def checkBoxField = getFieldById(fieldChanged) def checkBoxFieldValue = checkBoxField.value def isCollection = checkBoxFieldValue instanceof Collection log.debug "Instance of Collection: ${isCollection}" def total = 0 // If selected more than 1 field if (isCollection) { def checkBoxFieldValueList = checkBoxFieldValue as List log.debug "checkBoxFieldValue: ${checkBoxFieldValueList}" if (checkBoxFieldValueList.contains('House')) { total += 100 } if (checkBoxFieldValueList.contains('Car')) { total += 200 } if (checkBoxFieldValueList.contains('Insurance')) { total += 300 } // If selected 1 field only } else { def checkBoxFieldValueString = checkBoxFieldValue.toString() log.debug "checkBoxFieldValue: ${checkBoxFieldValueString}" if (checkBoxFieldValue == 'House') { total += 100 } if (checkBoxFieldValueString == 'Car') { total += 200 } if (checkBoxFieldValueString == 'Insurance') { total += 300 } } log.debug "Current total: ${textField.value}" log.debug "total: ${total}" textField.setFormValue(total)
Discovered an issue? Report it here