Set checklist options via Java API

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
Accepted answer

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.

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

 

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

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)

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);
    }
}

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

It works smile Thanks a lot for the quick support!

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

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
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,128 views 0 8
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