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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you instead provide a sample of the code you use to get the customfield and change its value?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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); } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
By the way, you may have to use Object instead of ChecklistItem if you don't import the definition first.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No problem Can you mark it as resolved as it may help out other users at some point. Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Whyves, can you please explain me how can I import ChecklistItem class in a custom plugin? I would like integrate with your plugin.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.