How to retrieve an array of possible multicheck options?

Thomas Orndorff January 28, 2014

Is it possible in Java to retrieve an array of possible options that a user is offered in a custom multi-check field?

For example, my multi-check field 'ManyOptions' has 3 true/false options: "Option1", "Option2", "Option3". I can successfully retrieve an array containing all options that are marked True ([ "Option1", "Option2" if the first and second are checked, for example).

If I could retrieve an array of all available options, then I can compare the two arrays to see if a user has checked all, some, or none.

I am using JIRA 5.2 with the 'Misc Custom Fields' plugin and with the following code:

<!-- @@Formula:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;

String sumCustomFieldValues( String customFieldId, Issue theIssue ) {
	CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
	CustomField customField = customFieldManager.getCustomFieldObject(customFieldId);
	
	String val = "";
	if(theIssue.getCustomFieldValue(customField) != null) {
	
		try {
			val = String.valueOf(theIssue.getCustomFieldValue(customField)).toString();
		} catch (Exception e) {
		}
	}
	
	val = theIssue.getCustomFieldValue(customField).getClass().getName();
	return val;
}

sumCustomFieldValues("customfield_10001", issue.getIssueObject());

-->

The returned value is dummied a bit to just return a String for debugging purposes.

1 answer

1 accepted

1 vote
Answer accepted
David _old account_
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.
January 28, 2014

You should look into com.atlassian.jira.issue.fields.CustomField#getOptions(java.lang.String, com.atlassian.jira.issue.fields.config.FieldConfig, com.atlassian.jira.issue.context.JiraContextNode). You call it on your custom field object, passing:

  • whatever (e.g. null) as the first param (not used)
  • a FieldConfig object you get from com.atlassian.jira.issue.fields.CustomField#getRelevantConfig(com.atlassian.jira.issue.Issue) passing your issue object
  • null as the last param. In reality, some custom field types will require the JiraContextNode, but I don't know how to get it. And since at least some field types ignore it, it might work for you
Thomas Orndorff January 28, 2014

Excellent, thank you. I've used this code successfully:

<!-- @@Formula:
 
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
 
String sumCustomFieldValues( String customFieldId, Issue theIssue ) {
    CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
    CustomField customField = customFieldManager.getCustomFieldObject(customFieldId);
     
	int currentOptionCount;
	int availableOptionCount;
	
    if(theIssue.getCustomFieldValue(customField) != null) {
		try {
			Object relConfig = customField.getRelevantConfig(theIssue);
			Object availableOptions = customField.getOptions(null, relConfig, null);	
			
			currentOptionCount = theIssue.getCustomFieldValue(customField).size();
			availableOptionCount = availableOptions.size();
        } catch (Exception e) {
        }
    }
	
	String estimationStatus;
	
	if (currentOptionCount == availableOptionCount){
		comparisonResult = "Complete";
	} else {
		comparisonResult = "Incomplete";
	}
	
    return comparisonResult;
}
 
sumCustomFieldValues("customfield_10001", issue.getIssueObject());
 
-->

David _old account_
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.
January 28, 2014

Cool.

Don't forget to "accept" an anwser, so that the Question can be considered as "Answered".

David

Suggest an answer

Log in or Sign up to answer