How to retrieve an array of possible multicheck options?

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

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

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

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
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 yesterday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

45 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