This documentation is for version 4.x. For the latest documentation, click here

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

A common use-case is to clear the checklist’s current content and build it from the ground up

First, you will need to create an empty collection (ArrayList<ChecklistItem) that will contain the new items.

def ArrayList<ChecklistItem> newChecklistValue = new ArrayList<ChecklistItem>();

You then create an item and add it to the collection.

// Create a new Checklist
def item = checklistCustomFieldType.getSingularObjectFromString('{"name": "item name"}');
newChecklistValue.add(item);

And you finally update the custom field passing it the newly created collection.

// Update the issue with the new checklist
checklistCustomFieldType.updateValue(checklistCustomField, event.issue, newChecklistValue);

Here is a complete example that can be used to update a checklist with a new set of items:

import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
@WithPlugin("com.okapya.jira.checklist")
import com.okapya.jira.customfields.*;

def ChecklistItem createChecklistItem(ChecklistCFType cfType, String name, boolean checked, String status, boolean mandatory, int rank) {
    def itemJson =  """
    {
        "name": "${name}",
        "checked": ${checked ? "true" : "false"},
        "statusId": "${status == null || status == "" ? "none" : status}",
        "mandatory": ${mandatory ? "true" : "false"},
        "rank": ${rank}
    }
    """;
    return cfType.getSingularObjectFromString(itemJson);
}

// Retrieve the Custom Field Type for the Checklist Custom Field
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def checklistCustomField = customFieldManager.getCustomFieldObject("customfield_10013");
def checklistCustomFieldType = (ChecklistCFType) checklistCustomField.getCustomFieldType();

// Create a new Checklist
def ArrayList<ChecklistItem> newChecklistValue = new ArrayList<ChecklistItem>();
newChecklistValue.add( createChecklistItem(checklistCustomFieldType, "Compile the solution", false, null, true, 0) );
newChecklistValue.add( createChecklistItem(checklistCustomFieldType, "Run all tests", true, "notApplicable", false, 1) );

// Update the issue with the new checklist
def issue = (MutableIssue) event.issue;
checklistCustomFieldType.updateValue(checklistCustomField, issue, newChecklistValue);
  • No labels