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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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;
	}

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

Can the same value be used twice?

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

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

Thank you, you saved a lot of my time

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.

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

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

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

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";

}

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

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

TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you