This documentation is for version 4.x. For the latest documentation, click here
Update a checklist when another field changes value
A popular use-case is to change the checklist items depending on the value of another field. For example, you may want to check the value of a select drop-down and then change the checklist.
You can achieve this by first creating a listener on the Issue Created
or Issue Updated
events. This will allow you to intercept the operation, retrieve the value of the conditional field and then build the checklist from there.
The checklist won’t be automatically updated in the edit dialog or issue view as it requires the user to first commit his changes in order for the groovy script to react to the event. To achieve real time update, you would need to write Javascript code on the web page but Checklist for Jira doesn’t currently offer a Javascript API.
For example, let’s assume that you are looking customfield_10015
to determine which items you should add to the checklist. You will first start by retrieving the custom field and extract its value.
def selectCustomField = customFieldManager.getCustomFieldObject("customfield_10015");
def selectValue = event.issue.getCustomFieldValue(selectCustomField);
Then you use the extracted value to determine which items to add.
def ArrayList<ChecklistItem> newChecklistValue = new ArrayList<ChecklistItem>();
switch(selectValue) {
case "Planning":
newChecklistValue.add( createChecklistItem(checklistCustomFieldType, "Call customers", true, null, false, 0) );
newChecklistValue.add( createChecklistItem(checklistCustomFieldType, "Survey customers", true, null, false, 1) );
break;
case "Deploying":
newChecklistValue.add( createChecklistItem(checklistCustomFieldType, "Commit to the prod branch", true, null, false, 0) );
break;
}
Following is a complete example that can be used to verify a Single Select field value and use it to build a checklist:
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();
// Find the Select Value
def issue = (MutableIssue) event.issue;
def selectCustomField = customFieldManager.getCustomFieldObject("customfield_10015");
def selectValue = issue.getCustomFieldValue(selectCustomField);
// Create a new Checklist with content depending on the Select value
def ArrayList<ChecklistItem> newChecklistValue = new ArrayList<ChecklistItem>();
switch(selectValue) {
case "Testing":
newChecklistValue.add( createChecklistItem(checklistCustomFieldType, "Run all tests", true, null, false, 0) );
break;
case "Development":
newChecklistValue.add( createChecklistItem(checklistCustomFieldType, "Compile solution", true, null, false, 0) );
break;
}
// Update the issue with the checklist
checklistCustomFieldType.updateValue(checklistCustomField, issue, newChecklistValue);