Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,364,456
Community Members
 
Community Events
168
Community Groups

How do you add custom field options in a plugin?

I'm currently trying to write a plugin for JIRA that, among other things, will add options to a Multi-Select type custom field I have based on data in an external database. I can't for the life of me figure out where, if at all, in the SDK I can add a new option to the custom field.

Basically, we have a field that is a multi-select list of our customers and I need to add new options to the field when we add customers into our CRM. Is this possible to do via the SDK or do I need to directly write to the database?

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted

The API definitely - if you add via SQL in the database, you need to stop Jira while you do it...

This might help. It's for v4, but I don't think the principles have changed much - create an "option" object and add it to the custom field. Note that I only needed this for the default/global context, you'll need to expand on the config-scheme bit if you need to use contexts.

public Option addSelectValue(String value, CustomField cf)
    {

    	long numberAdded = 0 ;
    	//System.out.println ("Try to add: " + value );
    	OptionsManager optionsManager = ManagerFactory.getOptionsManager() ;

    	Option newOption = null;

        if (cf != null)
        {
            List schemes = cf.getConfigurationSchemes();
            if (schemes != null && !schemes.isEmpty())
            {
                FieldConfigScheme sc = (FieldConfigScheme) schemes.get(0);
                Map configs = sc.getConfigsByConfig();
                if (configs != null && !configs.isEmpty())
                {
                    FieldConfig config = (FieldConfig) configs.keySet().iterator().next();
                    newOption = optionsManager.createOption(config,
                                                            null,
                                                            new Long(numberAdded),
                                                            value);
                    numberAdded++;
                    optionsManager.getOptions(config).sortOptionsByValue (null);
                }

            }
        }
        //System.out.println ("Should have added: " + newOption.getValue() );
        return newOption;
    }

As soon as I saw this I felt a little bit stupid as I swear I've done this before. Still not sure why I couldn't seem to find this in the docs; I was fully convinced there was an OptionsManager class. Oh well. Either way, thanks for the help. Both of you.

Use OptionsManager. Find the FiledConfig and use createOption method!

List <FieldConfigScheme> schemes = fieldConfigSchemeManager.getConfigSchemesForField(customField);
FieldConfig config = fieldConfigScheme.getOneAndOnlyConfig();

0 votes

The API definitely - if you add via SQL in the database, you need to stop Jira while you do it...

This might help. It's for v4, but I don't think the principles have changed much - create an "option" object and add it to the custom field. Note that I only needed this for the default/global context, you'll need to expand on the config-scheme bit if you need to use contexts.

public Option addSelectValue(String value, CustomField cf)
    {

    	long numberAdded = 0 ;
    	//System.out.println ("Try to add: " + value );
    	OptionsManager optionsManager = ManagerFactory.getOptionsManager() ;

    	Option newOption = null;

        if (cf != null)
        {
            List schemes = cf.getConfigurationSchemes();
            if (schemes != null && !schemes.isEmpty())
            {
                FieldConfigScheme sc = (FieldConfigScheme) schemes.get(0);
                Map configs = sc.getConfigsByConfig();
                if (configs != null && !configs.isEmpty())
                {
                    FieldConfig config = (FieldConfig) configs.keySet().iterator().next();
                    newOption = optionsManager.createOption(config,
                                                            null,
                                                            new Long(numberAdded),
                                                            value);
                    numberAdded++;
                    optionsManager.getOptions(config).sortOptionsByValue (null);
                }

            }
        }
        //System.out.println ("Should have added: " + newOption.getValue() );
        return newOption;
    }

TAGS

Atlassian Community Events