Skip to main content
Skip table of contents

Converting Checkboxes fields to Checklist fields using ScriptRunner

With ScriptRunner, you can import values from another custom field, such as Checkboxes, into a Checklist field.

Since Checkboxes custom fields use options as values, we suggest that you transfer the option values to global items. Global items are the successor to options in the Checklist app.


Steps

  1. Create global items with names that correspond to each of the Checkboxes field’s options.

  2. Get the Checklist field that you want to transfer the values to.

    GROOVY
    def customFieldManager = ComponentAccessor.getCustomFieldManager();
    
    // Change the field ID to your field's ID
    def checklistField = customFieldManager.getCustomFieldObject("customfield_10001");
    def checklistCFType = (ChecklistCFType) checklistField.getCustomFieldType();

  3. Get the Checkboxes field that you want to transfer the values from.

    GROOVY
    // Change the field ID to your field's ID
    def checkboxesField = customFieldManager.getCustomFieldObject("customfield_10002");

  4. Fetch the issues that you want to transfer values in. In the below example, we used ScriptRunner to perform a JQL query, but you can fetch the issues using the way that best suits your needs.

    GROOVY
    // The JQL query you want to search with
    final jqlSearch = "Some JQL query"
    
    def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser;
    def searchService = ComponentAccessor.getComponentOfType(SearchService);
    
    // Parse the query
    def parseResult = searchService.parseQuery(user, jqlSearch);
    if (!parseResult.valid) {
        log.error('Invalid query')
        return;
    }
    
    // Perform the query to get the issues
    def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
    def issues = results.results

  5. Loop over each issue and transfer the values from one field to another.

    GROOVY
    def issueManager = ComponentAccessor.getIssueManager();
    issues.each {issueSearchResult -> 
      // Issue objects fetched from JQL cannot be operated on. We need to obtain the right issue object.
      def issue = issueManager.getIssueObject(issueSearchResult.id);
      
      def checkboxesValue = (String[]) issue.getCustomFieldValue(checkboxesField);
      if (checkboxesValue == null) {
        // "null" means no options are checked in the Checkboxes field.
        // The Checkboxes field is empty, so we clear the Checklist values.
        checklistCFType.updateValue(checklistField, issue, null);
        return;
      }
      
      def items = (Collection<ChecklistItem>) issue.getCustomFieldValue(checklistField);
      // We loop through the items in the checklist and check those that are found in the Checkboxes values.
      // The idea here is that the Checkboxes values only contain checked options.
      for (ChecklistItem item : items) {
        item.setChecked(checkboxesValue.contains(item.getName()));
      }
      
      checklistCFType.updateValue(checklistField, issue, items);
    }

Full example

Here is a full example that imports a Checkboxes custom field into a Checklist custom field in issues obtained using a JQL query.

The script assumes that the Checklist field has global items with names that match exactly with the Checkboxes' options.

GROOVY
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
@WithPlugin("com.okapya.jira.checklist")
import com.okapya.jira.customfields.*;
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter

def customFieldManager = ComponentAccessor.getCustomFieldManager();

// Change the field ID to whatever ID your field has
def checklistField = customFieldManager.getCustomFieldObject("customfield_10001");
def checklistCFType = (ChecklistCFType) checklistField.getCustomFieldType();

// Change the field ID to whatever ID your field has
def checkboxesField = customFieldManager.getCustomFieldObject("customfield_10002");

// The JQL query you want to search with
final jqlSearch = "Some JQL query"

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser;
def searchService = ComponentAccessor.getComponentOfType(SearchService);

// Parse the query
def parseResult = searchService.parseQuery(user, jqlSearch);
if (!parseResult.valid) {
    log.error('Invalid query')
    return;
}

// Perform the query to get the issues
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
def issues = results.results

def issueManager = ComponentAccessor.getIssueManager();
issues.each {issueSearchResult -> 
  // Issue objects fetched from JQL cannot be operated on. We need to obtain the right issue object.
  def issue = issueManager.getIssueObject(issueSearchResult.id);
  
  def checkboxesValue = (String[]) issue.getCustomFieldValue(checkboxesField);
  if (checkboxesValue == null) {
    // "null" means no options are checked in the Checkboxes field.
    // In case of subsequent calls of this script, we set the value of the Checklist field to null too.
    checklistCFType.updateValue(checklistField, issue, null);
  }
  
  def items = (Collection<ChecklistItem>) issue.getCustomFieldValue(checklistField);
  // We loop through the items in the checklist and check those that are found in the Checkboxes values.
  // The idea here is that the Checkboxes values only contain checked options.
  for (ChecklistItem item : items) {
    item.setChecked(checkboxesValue.contains(item.getName()));
  }
  
  checklistCFType.updateValue(checklistField, issue, items);
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.