Check if Checkbox Contains a List of Wanted Values

Overview

This script can be used to detect when a checkbox field contains a given list of string values. If the condition is met, a wide range of actions can be done, such as updating another custom field or transitioning the issue.

Example

As a Product Manager, I want to be sure that all acceptance criteria has been met when the issue is updated. If all checkboxes have been clicked, then a message can be added to the issue indicating that all of them are completed.

Requirements

Jira Jira (7.7 - 8.6)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.atlassian.jira.issue.MutableIssue

// Set log level
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)

final fieldName = "CheckBoxA"
final wantedOptions = ["Yes", "No"]

def customFieldManager = ComponentAccessor.customFieldManager
def checkBoxFieldA = customFieldManager.customFieldObjects.find { it.name == fieldName }
def issue = event.issue as MutableIssue

// If checkbox field does not exist, it is not needed to continue
if (!checkBoxFieldA) {
    return
}

def checkBoxFieldAValue = issue.getCustomFieldValue(checkBoxFieldA)
// If checkbox does not contain values, it is not needed to continue
if (!(checkBoxFieldAValue in List)) {
    return
}

def selectedOptions = checkBoxFieldAValue?.collect { (it as LazyLoadedOption).value }
def containsAllWanted = selectedOptions.containsAll(wantedOptions)
log.debug("""
selected Options string list = $selectedOptions
Boolean check if contains wanted = $containsAllWanted
""")
if (containsAllWanted) {
    // Run the logic that you want to run when the checkbox fields contains all wanted values here
    // For example, all check box options that were selected can be cleared
    def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
    issue.setCustomFieldValue(checkBoxFieldA, null)
    ComponentAccessor.issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
Discovered an issue? Report it here

Suggested for you