Set checklist options via Java API

Slavina Sobadjieva March 31, 2016

Hi,

we are currently using the Checklist for JIRA plugin and for a project I need to pre-set the options of a Checklist custom field from a custom JIRA plugin. Is it possible to do so?

I tried setting the field value as JSON, String array and whatever I could think of but on issue update it always fails when trying to set the field ids (on create issue it just doesn't set any value for the field):

java.lang.ClassCastException: java.lang.String cannot be cast to com.okapya.jira.customfields.ChecklistItem at com.okapya.jira.customfields.ChecklistCFType.setItemIds(ChecklistCFType.java:399) at com.okapya.jira.customfields.ChecklistCFType.updateValue(ChecklistCFType.java:95) at com.okapya.jira.customfields.ChecklistCFType.updateValue(ChecklistCFType.java:50) at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:543) at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:504)

Thanks,

Slavina

2 answers

1 accepted

0 votes
Answer accepted
Yves Riel [Okapya]
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
March 31, 2016

Hi Slavina,

What do you want to do: Add items or modify current ones?

The easiest and safest way would be to use the method "getSingularObjectFromString" from the customfieldType. You can then pass this method a json value and it will convert it into a ChecklistItem which can then be saved properly.

0 votes
Slavina Sobadjieva March 31, 2016

Hey Whyves,

I want to add items - I need to populate the items on issue creation, so the field should be empty.

Do you have an example of how to use this "getSingularObjectFromString" method? Is it implemented by the ChecklistItem?

Thanks,

Slavina

 

Yves Riel [Okapya]
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
March 31, 2016

Can you instead provide a sample of the code you use to get the customfield and change its value?

Slavina Sobadjieva March 31, 2016

This is the function that I use for creating the issue - I tried also setting the checklist field to "* Option\n* 2nd Optioin" or just "Option" but didn't work

private void createIssueByDeliveryName(Issue epic, String deliveryName) {
	CustomField cfEpicLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(Constants.CF_EPIC_LINK);
    CustomField cfSoChecklist = cfSoChecklist = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(Constants.CF_SO_CHECKLIST);
        
    Issue DirectOrderIssue = null;
	Project proj = ComponentAccessor.getProjectManager().getProjectObjByName(deliveryName);
	MutableIssue issueObject = issueFactory.getIssue();
	issueObject.setProjectObject(proj);
	issueObject.setIssueTypeId("21");
	issueObject.setPriorityObject(ComponentAccessor.getConstantsManager().getDefaultPriorityObject());
	issueObject.setSummary(getSummary());
	issueObject.setCustomFieldValue(cfEpicLink, epic);
	issueObject.setCustomFieldValue(cfSoChecklist, "{\"checked\":true,\"name\":\"testing\",\"mandatory\":true,\"rank\":0,\"optionId\":0,\"version\":\"3.0\",\"id\":1,\"statusId\":\"none\"}");
	
	issueObject.setDescription("Sample description");
	try {
		Map<String, Object> params = new HashMap<>();
		params.put("issue", issueObject);
		DirectOrderIssue = issueManager.createIssueObject(user, params);
	} catch (Exception e) {
		log.debug(deliveryName + " was not created. Exception is " + e);
	}
}

for update I tried the following:

ApplicationUser usr = userManager.getUserByName("usr");
MutableIssue issueObject = issueManager.getIssueObject("TST-101");
ArrayList<String> tmp = new ArrayList<>();
tmp.add('{"checked":true,"name":"lalal","mandatory":true,"rank":0,"optionId":0,"version":"3.0","id":1,"statusId":"none"}');
issueObject.setCustomFieldValue(cfSoChecklist, tmp);
issueManager.updateIssue(ApplicationUsers.toDirectoryUser(usr), issueObject, EventDispatchOption.ISSUE_UPDATED, false)
Yves Riel [Okapya]
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
March 31, 2016

Try something like this (not tested). You need to get the customfield type first and then use it to create new ChecklistItems from json notation. Don't try to set the new checklist id yourself, the customfield should take car of this. Otherwise, you may end up with issues.

private void createIssueByDeliveryName(Issue epic, String deliveryName) {
    CustomField cfEpicLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(Constants.CF_EPIC_LINK);
    CustomField cfSoChecklist = cfSoChecklist = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(Constants.CF_SO_CHECKLIST);
    CustomFieldType cfSoChecklistType = cfSoChecklist.getCustomFieldType()
         
    Issue DirectOrderIssue = null;
    Project proj = ComponentAccessor.getProjectManager().getProjectObjByName(deliveryName);
    MutableIssue issueObject = issueFactory.getIssue();
    issueObject.setProjectObject(proj);
    issueObject.setIssueTypeId("21");
    issueObject.setPriorityObject(ComponentAccessor.getConstantsManager().getDefaultPriorityObject());
    issueObject.setSummary(getSummary());
    issueObject.setCustomFieldValue(cfEpicLink, epic);
    Collection<ChecklistItem> checklistItems = new ArrayList<ChecklistItem>();
    ChecklistItem checklistItem = cfSoChecklistType.getSingularObjectFromString("{\"checked\":true,\"name\":\"testing\",\"mandatory\":true,\"rank\":0,\"optionId\":0,\"statusId\":\"none\"}")
    checklistItems.add(checklistItem);
    issueObject.setCustomFieldValue(cfSoChecklist, checklistItems);
     
    issueObject.setDescription("Sample description");
    try {
        Map<String, Object> params = new HashMap<>();
        params.put("issue", issueObject);
        DirectOrderIssue = issueManager.createIssueObject(user, params);
    } catch (Exception e) {
        log.debug(deliveryName + " was not created. Exception is " + e);
    }
}
Yves Riel [Okapya]
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
March 31, 2016

By the way, you may have to use Object instead of ChecklistItem if you don't import the definition first.

Slavina Sobadjieva March 31, 2016

It works smile Thanks a lot for the quick support!

Yves Riel [Okapya]
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
March 31, 2016

No problem smile Can you mark it as resolved as it may help out other users at some point. Thanks!

Fidel Castro
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 11, 2016

Whyves, can you please explain me how can I import ChecklistItem class in a custom plugin? I would like integrate with your plugin.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events