The Checklist custom field can be programmatically manipulated to extract and modify its data.
You can use any scripting tool that allows you to access the custom field’s Java-based classes, but one of the most popular tools for this is ScriptRunner, which uses Groovy scripts. All the examples provided in this section have been built and tested with ScriptRunner, but should still work with other automation tools.
On this page:
Table of Contents | ||
---|---|---|
|
Using Checklist classes and APIs
Every custom field in Jira is based on a custom field type. The class of the Checklist custom field type is ChecklistCFType
, which extends Jira’s native Multi Custom Field Type. Besides the ChecklistCFType
class, another very important class is ChecklistItem
, which represents a checklist item.
Class | Package |
---|---|
ChecklistCFType | com.okapya.jira.customfields |
ChecklistItem | com.okapya.jira.customfields |
While many operations can be performed on the custom field directly, such as extracting and updating checklist data, some operations require access to the custom field type.
Here are a few tips for interacting with the Checklist custom field:
The data that the custom field returns is a collectionof
ChecklistItem
.When you update a checklist, you also need to provide a collectionof
ChecklistItem
.To find a specific checklist item, you must iterate through the collection of
ChecklistItem
.To create a new
ChecklistItem
, it’s best to use thegetSingularObjectFromString(string)
method of theChecklistCFType
class. The input string to the method is a JSON representation of a checklist item.
ChecklistCFType useful methods
Method | Return value | Description |
---|---|---|
| ChecklistItem | This is the best way to create a new checklist item. |
| Void | Updates the custom field value directly without dispatching any events or generating any change logs. |
ChecklistItem public methods
Method | Return value | Description | ||||||
---|---|---|---|---|---|---|---|---|
| ChecklistItem | An alternate way to build a checklist item from a JSON string. | ||||||
| String | Returns the due date formatted in the desired format (see the Available formats). | ||||||
| List<String> | The usernames of the users to which the item is assigned. | ||||||
| String | The due date (and time) of the item in ISO format. | ||||||
| Integer | The ID of the global item (previously known as an “option”), if applicable, otherwise -1 is returned. | ||||||
| Integer | Unique identifier for the item. | ||||||
| Boolean | Whether the item is a header or not. | ||||||
| String | The name of the item, including Markdown and the description. To learn more about Markdown and descriptions, see Using special formatting and Adding descriptions to items or headers. | ||||||
| String | The name of the item, without the description. Passing | ||||||
| Long |
Use | ||||||
| String | The numeric priority ID of the item as a string. | ||||||
| String | The name of the priority assigned to the item. This searches Jira’s priority scheme. If the item’s priority ID is not found in the scheme, an empty string will be returned. | ||||||
| Integer | The rank of the item in the checklist. | ||||||
| String | The status ID of the item ( | ||||||
| Boolean | Whether the item has a status or not. If | ||||||
| Boolean | Whether the item is checked or not. | ||||||
| Boolean | Whether the linked global item (previously known as an “option”) is disabled or not. | ||||||
| Boolean | Whether the item is a global item (previously known as an “option”) or not. | ||||||
| Boolean | Whether the item is mandatory or not. | ||||||
| Boolean |
Use | ||||||
| Void | Changes the users to which the item is assigned. To learn more about assignees, see Assigning users to items. Only the first assignee is taken into account. | ||||||
| Void | Changes the checked state of the item. | ||||||
| Void |
Use | ||||||
| Void | Changes the due date of the item. | ||||||
| Void | Changes whether the item is a header or not. | ||||||
| Void | Changes the mandatory state of the item. | ||||||
| Void | Changes the item name, including Markdown and the description, as required. To learn more about Markdown and descriptions, see Using special formatting and Adding descriptions to items or headers. | ||||||
| Void | Changes the priority ID of the item. | ||||||
| Void | Changes the rank of the item. Global item ranks cannot be changed. Setting an item’s rank to a rank already in use by another item will have unpredictable results. The changed item may end up before or after the other item. | ||||||
| Void | Changes the status ID of the item. | ||||||
| String | Returns the JSON representation of the item. |
ChecklistItem JSON representation
This JSON structure differs slightly from the JSON structure in Modifying checklists using a REST API. Use the following structure when creating a ChecklistItem
:
JSON property | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
| Number | The unique identifier, which can be ignored when creating items. | ||||||
| Number |
Replaced by | ||||||
| Number | The identifier of the global item, which only needs to be provided if you want to modify a global item. | ||||||
| String | The name of the item; this is the only mandatoryproperty to create a checklist item. | ||||||
| Boolean | Whether the item is checked or not. | ||||||
| Boolean | Whether the item is a header or not. | ||||||
| String Array | The list of the usernames to which the item is assigned (only one assignee is currently supported). | ||||||
| String | The due date of the item in ISO format. | ||||||
| String | The numeric priority ID of the item as a string. | ||||||
| Boolean | Whether the item is mandatory or not. | ||||||
| Number | The order of the item. | ||||||
| String | The status ID of the item ( |
Creating ChecklistItem objects using "getSingularObjectFromString"
The best way to create a ChecklistItem
object is to use the getSingularObjectFromString(String)
method of the custom field type. The input to the method is a string containing a JSON representation of the item.
Code Block | ||
---|---|---|
| ||
import com.atlassian.jira.component.ComponentAccessor; // Retrieve the custom field type for the Checklist custom field def customFieldManager = ComponentAccessor.getCustomFieldManager(); def checklistCustomField = customFieldManager.getCustomFieldObject("customfield_10013"); def checklistCustomFieldType = checklistCustomField.getCustomFieldType(); // JSON representation of an item def itemJson = ''' { "name": "Run all tests", "checked": false, "statusId": "none", "mandatory": false, "rank": 0 } '''; // Create a checklist item def ChecklistItem newItem = checklistCustomFieldType.getSingularObjectFromString(itemJson); |
Utility method
If you need to create many checklist items, using a utility method is a good practice and will make creating ChecklistItem
objects easier.
Code Block | ||
---|---|---|
| ||
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); } |
Creating your first Groovy script
Get access to the Checklist classes in your script by adding the following statements in the import section:
Code Block language java import com.onresolve.scriptrunner.runner.customisers.WithPlugin; @WithPlugin("com.okapya.jira.checklist") import com.okapya.jira.customfields.*;
Access the Checklist custom field and use it to get the checklist items from the issue.
Code Block language java import com.atlassian.jira.component.ComponentAccessor; // Retrieve the Custom Field Type for the Checklist Custom Field def customFieldManager = ComponentAccessor.getCustomFieldManager(); def checklistCustomField = customFieldManager.getCustomFieldObject("customfield_10013"); // Get the Checklist value. In this example, the issue is taken from inside a Scriptrunner listener def issue = event.issue; def ArrayList<ChecklistItem> existingChecklistValue = (ArrayList<ChecklistItem>) issue.getCustomFieldValue(checklistCustomField);
Loop over the checklist’s value to find out whether any items have been checked.
Code Block language java def anyItemChecked = false; // Find out if any item is checked for (ChecklistItem item : existingChecklistValue) { if (item.isChecked()) { anyItemChecked = true; break; } }
Congratulations! You now have the basic building blocks to programmatically access a checklist. Next, try powering up your scripting skills even further with the other procedures provided in this section:
Page Tree | ||
---|---|---|
|
Status | ||
---|---|---|
|