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

This widget could not be displayed.

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
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

268 views 1 3
Join discussion

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