Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The following example It is a common use-case to reset the checklist completely and build it from the ground up. This page will demonstrate how to achieve it.

...

To assign a new set of items, you must create a new ArrayList<ChecklistItem>:

Code Block
// Create a new Checklist
def ArrayList<ChecklistItem> newChecklistValue = new ArrayList<ChecklistItem>();
def item = checklistCustomFieldType.getSingularObjectFromString('{"name": "item name"}');
newChecklistValue.add(item);

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

...

Here is a complete example that updates the checklist with a completely new list set of items:

Code Block
languagejava
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);