Populating the JIRA Checklist plugin via groovy scriptrunner

I am trying to populate the JIRA checklist plugin with a bunch of hard coded values using Post Functions, i think I have the necessary code to make it work, but I am trying to figure out what kind of Object the CheckList Plugin takes so it can be populated.

def cf = customFieldManager.getCustomFieldObjectByName("Checklist_Field")
def cfConfig = cf.getRelevantConfig(issue)
value = // JIRA CHECKLIST OBJECT?
issue.setCustomFieldValue(cf, value)

It doesn't seem to work when i use a List with string values. I am aware it needs a checked, and mandatory boolean along with a String for the name of the option, but I am not sure how to format it. I have tried using the OptionsManager using similar code to this: 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
def optionManager = ComponentAccessor.getOptionsManager()
def option = optionManager.allOptions.find {it.value == request_type_value6}
if (option) {
// this option already exists
} else {
    def newOption = optionManager.createOption(request_type7.getRelevantConfig(issue), null, 100, request_type_value6)
}

but that didnt work either.

Any suggestions would be great.  

3 answers

1 accepted

Hi Joshi,

The value returned by the customfield is of a specific type (ChecklistItem) which means that pure strings will not work. When creating an item, to convert a json string to a CheklistItem, you need to get the customefieldType from the customfield and call getSingularObjectFromString. The string needs to be a json notation in the form

{"name" : "My Checklist Item", "checked" : true, "mandatory" : false}

You can then save the customfield value. When saving the customfield value, it needs to be a collection of ChecklistItems.

If you simply need to edit a checklist item, keep it in its ChecklistItem form and use its accessors.

By the way, not sure if you are aware but Checklist offers Post Workflow functions to change a checklist.

Just a quick question, how would you go about adding multiple items to the checklist?

I tried doing this: 

 

{"name" : "My Checklist Item", "checked" : true, "mandatory" : false},{"name" : "Another Item", "checked" : true, "mandatory" : false},{"name" : "A Third Item", "checked" : true, "mandatory" : false}

I passed the above json into the getSingularObjectFromString, but that didnt work. Is there another method to use?

 

cheers

Instead try to call getSingularObjectFromString for each json item and add the returned object to a collection. Then pass the collection to setCustomfieldValue.

cheers, that worked. 

Excellent thank you @Whyves it worked, 

 

This was the code I used to get it working:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def cf = customFieldManager.getCustomFieldObjectByName("Pre-Release Task");
def cfConfig = cf.getRelevantConfig(issue)
def cfType = cf.getCustomFieldType()
def value = cfType.getSingularObjectFromString('{"name" : "My Checklist Item", "checked" : true, "mandatory" : false}')
issue.setCustomFieldValue(cf, [value])

 

 

@Joshi Shushruth do you think it's possible to show the code you used to add multiple items to the checklist? 

Thanks a lot in advance, this is really useful!

def value1 = cfType.getSingularObjectFromString('{"name" : "Item1", "checked" : true, "mandatory" : false}')
def value2 = cfType.getSingularObjectFromString('{"name" : "Item2", "checked" : true, "mandatory" : true}')
def value3 = cfType.getSingularObjectFromString('{"name" : "Item3", "checked" : true, "mandatory" : false}')
def value4 = cfType.getSingularObjectFromString('{"name" : "Item4", "checked" : true, "mandatory" : false}')
log.warn " CRT value = ${crt_value} and CRT = ${crt}"
if (crt_value == "Request type")
{
 log.warn " CRT value = ${crt_value}"
issue.setCustomFieldValue(cf, [value1,value2,value3,value4])
}

 

Above code is working for adding multiple values in check list but issue is it is not maintaning order of each values.

It is changing order of items randomly 

I tried with ArrayList also but still not able to maintain order

Please help in knowing how to maintain order of items like

item1

item2

item3

...

..

Usually, it will rank them in the order they are received. I'm surprised that an array list didn't work.

If you want to force a rank, add the "rank" property to the json string.

{"name" : "Item4", "checked" : true, "mandatory" : false, "rank": 0}

Hi Whyves,

Thanks for your help and I appriciate your quick turnaround

It is working fine. Just adding code for others to help

I am new to Groovy scripting, since i worked as only Jira Admin , so kindly provide any document link in which, it has all properties and syntex etc like you mentioned above for rank.

Thanks in advance
Balvant

def value1 = cfType.getSingularObjectFromString('{"name" : "Item1", "checked" : true, "mandatory" : false , "rank" : 0}')
def value2 = cfType.getSingularObjectFromString('{"name" : "Item2", "checked" : true, "mandatory" : true  , "rank" : 1}')
def value3 = cfType.getSingularObjectFromString('{"name" : "Item3", "checked" : true, "mandatory" : false , "rank" : 2}')
def value4 = cfType.getSingularObjectFromString('{"name" : "Item4", "checked" : true, "mandatory" : false , "rank" : 3}')
def value5 = cfType.getSingularObjectFromString('{"name" : "Item5", "checked" : true, "mandatory" : false , "rank" : 4}')
def value6 = cfType.getSingularObjectFromString('{"name" : "Item6", "checked" : true, "mandatory" : true  , "rank" : 5}')
def value7 = cfType.getSingularObjectFromString('{"name" : "Item7", "checked" : true, "mandatory" : false , "rank" : 6}')
def value8 = cfType.getSingularObjectFromString('{"name" : "Item8", "checked" : true, "mandatory" : false , "rank" : 7}')

List item = new ArrayList();
item.add(value1)
item.add(value2)
item.add(value3)
item.add(value4)
item.add(value5)
item.add(value6)
item.add(value7)
item.add(value8)

log.warn " CRT value = ${crt_value} and CRT = ${crt} before insert"
if (crt_value == "Request_Type")
{ 
 issue.setCustomFieldValue(cf, item)
 log.warn "done with adding items ${item}"
}


 

 

Hi Whyves,

One small doubt, If we want to append few more values to existing Checklist because setCustomFieldValue is deleting all existing values and adding new values.

Suppose if i want to add item9, Item10, Item11 in next transition 

Thanks in advance

Balvant

If you want to manipulate an existing list, you should get the customfield value first via its standard API (see other example on the forum) and then manipulate the list. You should avoid always rewriting the entire list.

So in essence:

  1. issue.getCustomfieldValue(customfield) will return a list of ChecklistItems
  2. Add/remove items to the list
  3. Save list to customfield.

Thanks for your help and suggestions.

0 vote

To find the object type I usually retrieve an existing value and call getClass().getName() on that object

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

160 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you