Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How do I programatically create and add options to custom fields of type select and radiobutton?

Michael Danielsson
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.
October 18, 2011

Hi

Can someone give an example of how to programatically create and add options to custom fields of type select and radiobutton?

Regards

Michael Danielsson

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

10 votes
Answer accepted
Michael Danielsson
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.
November 1, 2011

I have now managed to create and add options to a Select field.

Can someone explain what numberAdded is used for?

Please comment if there are better ways to do this. The code is written for JIRA 4.3.3.

/**
	 * Create a customfield of type Select.
	 * 
	 * @param name
	 * @param description
	 * @throws GenericEntityException 
	 */
	public void createCFSelect(String name, String description, String issueType) throws GenericEntityException {
		handler.createCustomField(name, description, CustomFieldConstants.SELECT_FIELD_TYPE, CustomFieldConstants.SELECT_FIELD_SEARCHER, handler.getIssueTypeFromName(issueType));
	}
	

	/**
	 * Create a customfield of type Radio.
	 * 
	 * @param name
	 * @param description
	 * @throws GenericEntityException 
	 */
	public void createCFRadio(String name, String description, String issueType) throws GenericEntityException {
		handler.createCustomField(name, description, CustomFieldConstants.RADIO_FIELD_TYPE, CustomFieldConstants.RADIO_FIELD_SEARCHER, handler.getIssueTypeFromName(issueType));
	}

public CustomField createCustomField(final String customFieldName,
			final String description, @Nullable final String type,
			@Nullable final String searcherType, IssueType issueType) throws GenericEntityException {

		CustomFieldManager customFieldManager = getCustomFieldManager();

		// Create cf of the correct type
		CustomFieldType cfType;
		CustomFieldSearcher searcher = null;

		if (StringUtils.isNotEmpty(type)) {
			cfType = customFieldManager.getCustomFieldType(type);
		} else {
			cfType = customFieldManager
					.getCustomFieldType(CustomFieldConstants.TEXT_FIELD_TYPE);
		}

		if (StringUtils.isNotEmpty(searcherType)) {
			searcher = customFieldManager.getCustomFieldSearcher(searcherType);
		} else {
			List searchers = customFieldManager.getCustomFieldSearchers(cfType);
			if (searchers != null && !searchers.isEmpty()) {
				searcher = (CustomFieldSearcher) searchers.get(0);
			}
		}
		
		List issueTypeList = EasyList.buildNull();
		if (issueType != null) {
			issueTypeList = EasyList.build(issueType.getGenericValue());   
		}
		final CustomField customField = customFieldManager.createCustomField(
				customFieldName, description, cfType, searcher,
				EasyList.build(GlobalIssueContext.getInstance()),
				issueTypeList);

		return customField;
	}

		/**
	 * Get the custom field manager.
	 * 
	 * @return The custom field manager
	 */
	private CustomFieldManager getCustomFieldManager() {
		CustomFieldManager customfieldmanager = ComponentManager.getInstance()
				.getCustomFieldManager();
		return customfieldmanager;
	}

	
	private OptionsManager getOptionsManager() {
		OptionsManager optionsManager = ComponentManager
				.getComponentInstanceOfType(OptionsManager.class);
		return optionsManager;
	}

	public Option addOptionToCustomField(CustomField customField, String value) {
		Option newOption = null;

		if (customField != null) {
			List<FieldConfigScheme> schemes = customField
					.getConfigurationSchemes();
			if (schemes != null && !schemes.isEmpty()) {
				FieldConfigScheme sc = schemes.get(0);
				Map configs = sc.getConfigsByConfig();
				if (configs != null && !configs.isEmpty()) {
					FieldConfig config = (FieldConfig) configs.keySet()
							.iterator().next();

					OptionsManager optionsManager = getOptionsManager();
					Options l = optionsManager.getOptions(config);
					Option opt;

					int numberAdded = 100;
					newOption = optionsManager.createOption(config, null,
							new Long(numberAdded), // TODO What is this
							value);
				}
			}
		}

		return newOption;
	}

Jobin Kuruvilla [Adaptavist]
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.
November 1, 2011

It is the sequence in which the otions are displayed in the user interface. Higher the value, lower the position of the added option.

Michael Danielsson
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.
November 3, 2011

Can the same value be used twice?

Barry Britnell December 7, 2011

Where is the CustomFieldConstants class? I can't find it in the API. Thanks.

tommy guo
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.
June 5, 2013

Thank you for the very useful code! To clarify numberAdded=100 means that the option will be the 100th option in the list?

Mariusz Skierski September 15, 2013

Thank you, you saved a lot of my time

ramesh_babu NA October 17, 2013

Thanks, I am a newbie on Jira development, and it really helped and it worked. I was expecting some thing like optionManager.update() or something to presist , but

newOption = optionsManager.createOption(config, null,
                        new Long(numberAdded), 
                        value);

did the job.

Ravi Kiran December 22, 2013

Hi i am new to JIRA development environment and i have similar requirement to create values for custom fields programatically.so please guide me how/where to add this function saveValue and how to execute this?Thanks

Patrick van der Rijst
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.
August 12, 2014

Very tricky! What if a custom field uses multiple contexts?

Gopinath Velayudhan November 6, 2014

Hi, I am also looking for a way to update the option in select list. what do i have to do to make the above script run. Do i have to build a plugin. Can i just create a groovy or Java code and run the class external to Jira? Sorry for the newbie question. But how can i implement this without creating a plugin.

Like Eva Fernandez likes this
2 votes
Michael Danielsson
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.
December 8, 2011

import com.atlassian.jira.web.action.admin.customfields.CreateCustomField;

public class CustomFieldConstants {

private static final String SEARCHER = "searcher";

public static final String TEXT_FIELD_TYPE = CreateCustomField.FIELD_TYPE_PREFIX
+ "textfield";

public static final String TEXT_FIELD_SEARCHER = CreateCustomField.FIELD_TYPE_PREFIX
+ "textsearcher";

public static final String FREE_TEXT_FIELD_TYPE = CreateCustomField.FIELD_TYPE_PREFIX
+ "textarea";

public static final String DATE_FIELD_TYPE = CreateCustomField.FIELD_TYPE_PREFIX
+ "date";

public static final String DATETIME_FIELD_TYPE = CreateCustomField.FIELD_TYPE_PREFIX
+ "datetime";

public static final String DATETIME_FIELD_SEARCHER = CreateCustomField.FIELD_TYPE_PREFIX
+ "datetimerange";

public static final String DATE_PICKER_FIELD_TYPE = CreateCustomField.FIELD_TYPE_PREFIX
+ "datepicker";

public static final String DATE_FIELD_SEARCHER = CreateCustomField.FIELD_TYPE_PREFIX
+ "daterange";

public static final String SELECT_FIELD_TYPE = CreateCustomField.FIELD_TYPE_PREFIX
+ "select";

public static final String SELECT_FIELD_SEARCHER = SELECT_FIELD_TYPE
+ SEARCHER;

public static final String USER_PICKER_FIELD_TYPE = CreateCustomField.FIELD_TYPE_PREFIX
+ "userpicker";

public static final String USER_PICKER_SEARCHER = USER_PICKER_FIELD_TYPE
+ SEARCHER;

public static final String MULTISELECT_FIELD_TYPE = CreateCustomField.FIELD_TYPE_PREFIX
+ "multiselect";

public static final String MULTISELECT_FIELD_SEARCHER = MULTISELECT_FIELD_TYPE
+ SEARCHER;

public static final String NUMBER_FIELD_TYPE = CreateCustomField.FIELD_TYPE_PREFIX
+ "float";

public static final String NUMBER_FIELD_SEARCHER = CreateCustomField.FIELD_TYPE_PREFIX
+ "exactnumber";

public static final String NUMBER_RANGE_FIELD_SEARCHER = CreateCustomField.FIELD_TYPE_PREFIX
+ "numberrange";

public static final String MULTICHECKBOXES_FIELD_TYPE = CreateCustomField.FIELD_TYPE_PREFIX
+ "multicheckboxes";

public static final String MULTICHECKBOXES_FIELD_SEARCHER = CreateCustomField.FIELD_TYPE_PREFIX
+ "checkboxsearcher";

public static final String RADIO_FIELD_TYPE = CreateCustomField.FIELD_TYPE_PREFIX
+ "radiobuttons";

public static final String RADIO_FIELD_SEARCHER = CreateCustomField.FIELD_TYPE_PREFIX
+ "radiosearcher";

public static final String PLUGIN_KEY_PREFIX = "com.atlassian.jira.plugins.jira-importers-plugin:";

public static final String BUGZILLA_ID_TYPE = PLUGIN_KEY_PREFIX
+ "bug-importid";

public static final String BUGZILLA_ID_SEARCHER = CreateCustomField.FIELD_TYPE_PREFIX
+ "exactnumber";

public static final String SINGLE_VERSION_PICKER_TYPE = CreateCustomField.FIELD_TYPE_PREFIX
+ "version";

public static final String VERSION_PICKER_TYPE = CreateCustomField.FIELD_TYPE_PREFIX
+ "multiversion";
public static final String URL_FIELD_TYPE = CreateCustomField.FIELD_TYPE_PREFIX
+ "url";
public static final String URL_FIELD_SEARCHER = CreateCustomField.FIELD_TYPE_PREFIX
+ "urlsearcher";

}

1 vote
Bernhard Gruenewaldt April 24, 2014

I wrote a plugin to add/remove/edit customfield values.

The plugin provides a REST API. It works from Jira 6.2+

TAGS
AUG Leaders

Atlassian Community Events