Getting started with ScriptRunner
In this section, we will provide you with the necessary tools to build scripts with Checklist.
Import the Checklist Java classes
Import the Checklist Java classes using the following statements:
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
@WithPlugin("com.okapya.jira.checklist")
import com.okapya.jira.customfields.*;
Interacting with checklist values
There are two main methods for getting checklist items. You may encounter a situation where one method doesn’t work well with your use case and the code you are writing. To get an idea of which method you should use, refer to the table below.
Method | Fetching data | Updating data |
---|---|---|
|
|
|
|
|
|
Choose wisely between these two methods based on your use case. For instance, you should not use the IssueManager
to save data inside an Issue Updated
event listener because it will likely create an infinite loop.
Getting items from issues
The following examples show how to get items from issues using the previously mentioned methods.
For both methods, start by getting the CustomFieldManager
, which will help you get the CustomField
object that represents the Checklist field that you want the items for.
From the issue
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import com.atlassian.jira.component.ComponentAccessor;
@WithPlugin("com.okapya.jira.checklist")
import com.okapya.jira.customfields.*;
// The issue isn't defined in this example because it can come from many sources.
// See ScriptRunner's documentation to know where the issue object should come from: https://docs.adaptavist.com/sr4js/latest
def customFieldManager = ComponentAccessor.getCustomFieldManager();
// Change the ID to whatever ID your field has.
def checklistField = customFieldManager.getCustomFieldObject("customfield_10013");
Collection<ChecklistItem> items = (Collection<ChecklistItem>) issue.getCustomFieldValue(checklistField);
From the ChecklistCFType
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import com.atlassian.jira.component.ComponentAccessor;
@WithPlugin("com.okapya.jira.checklist")
import com.okapya.jira.customfields.*;
// The issue isn't defined in this example because it can come from many sources.
// See ScriptRunner's documentation to know where the issue object should come from: https://docs.adaptavist.com/sr4js/latest
def customFieldManager = ComponentAccessor.getCustomFieldManager();
// Change the ID to whatever ID your field has.
def checklistField = customFieldManager.getCustomFieldObject("customfield_10013");
def checklistCFType = (ChecklistCFType) checklistField.getCustomFieldType();
Collection<ChecklistItem> items = (Collection<ChecklistItem>) checklistCFType.getValueFromIssue(checklistField, issue);
Updating items in issues
The following examples show how to update items in issues using the previously mentioned methods.
With IssueManager
With ChecklistCFType
Loop through checklist items
Once you have obtained your Checklist field’s items, you can loop through them to either update or validate them.