How can I populate a multi-select custom field with values from an external source?

Don Frazier November 15, 2011

I have a multi-select custom field called "Client Codes". This list is growing and managing it is a manual task. Additionally, we have other JIRA screens that have similar client code fields that will need the same values. I want to be able to import a list of client codes from an external source (file or db) and have them updated in my custom fields. I looked into SQL Feed, however, it does not support multi-select. I looked at JIRA's CLI but it looks like I would need to write sql to insert the values in the tables and I am not clear how CLI would handle inserting new rows and the sequence values.

How have others accomplished this? Any advice would be greatly appreciated.

7 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

9 votes
Answer accepted
JamieA
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 17, 2011

Another alternative. The following javascript can be pasted in to the description of a short text field. It retrieves values from an external source, converts the text field to a select list, and populates the select options. Advantage over the other two solutions is it's comparitively simple and lightweight.

The file read from the other webserver contains the options, in JSONP format:

var fieldId = "customfield_10030";
function fieldOptions(data) {
    console.log (data);
    var currentValue = AJS.$("#" + fieldId).val();
    var select = AJS.$("select#" + fieldId);
    var foundIt = false;
    select.append("<option></option>");
    AJS.$.each(data, function(index, option) {
        console.log(option);
        if (option == currentValue) {
            select.append("<option selected>" + option + "</option>");
            foundIt = true;
        }
        else {
            select.append("<option>" + option + "</option>");
        }
    });

    if (!foundIt) {
        select.append("<option>" + currentValue + "</option>");
    }

    AJS.$("input#" + fieldId).remove();
}

AJS.$(document).ready(function() {
    console.log("val: " + AJS.$("#" + fieldId).val());
    AJS.$("#" + fieldId).parent().append("<select class='select' id='" + fieldId + "' name='" + fieldId + "'></select>");
    AJS.$("input#" + fieldId).hide();
    AJS.$.ajax({
            url: "http://some.web.server/fieldOptions.js?callback=fieldOptions",
            dataType: "jsonp"
    });

});

MattS
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 30, 2011

Hi Jamie,

That's need code but what if the selected option's value is not currently a valid option for that custom field? Doesn't JIRA choke on that?


~Matt

JamieA
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 30, 2011

Hi Matt,

The type of field is short text field, hence no options. The only downside is that you don't get a select list searcher, and can't rename options etc.

srinivasp
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 2, 2012

Hi Jamie,

You approach for rendering customfield options looks very simple. I am using jira 4.4.5. Can you please let me know what is the exact URL used for retriving the values of a specific custom field?

David Keane June 10, 2015

Hi Jamie this is exactly what I need, can you explain where this code goes please??

3 votes
Andrey Markelov
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.
July 23, 2013

You can use free plugin https://marketplace.atlassian.com/plugins/ru.andreymarkelov.atlas.plugins.requestedfields which provides two fields that maybe filled from HTTP response.

1 vote
Wojciech Seliga
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 17, 2011

If you don't mind writing a simple plugin on the server side (e.g. reading your client codes from a file or directly from a DB), then take it should be an easy task.

com.atlassian.jira.issue.customfields.manager.OptionsManager and com.atlassian.jira.issue.customfields.option.Options should give you everything you need.

Please note that in JIRA 4.4 options were quite redesigned and now they are not identified any longer by their string value (which made then uneditable), but by a regular id.

It also means that you should not freely remove options, because your existing issues will be affected. In you sync process you should just add new options.

Alternatively you may consider, shutting down JIRA and adding options directly to JIRA DB. I think that adding rows to customfieldoption table would be enough.

0 votes
Bernhard Gruenewaldt April 24, 2014

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

You could use the REST Api to store values from an external source for the customfield.

It works from Jira 6.2+

0 votes
Cyrille Martin
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.
July 29, 2012

Hi,

Sorry to answer 6 months after your question, but you can use nFeed to set multi-select customfied with data coming form external database, remote file or web service.

Regards

Cyrille

0 votes
Tom House December 8, 2011

what library / jar file contains the imported classes? I created a WS client via jira's wsdl but I think I need a jar file that contains the classes you're using.

import com.atlassian.jira.bc.project.component.ProjectComponent;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.customfields.impl.MultiSelectCFType;
import com.atlassian.jira.issue.customfields.manager.GenericConfigManager;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.issue.customfields.persistence.CustomFieldValuePersister;
import com.atlassian.jira.issue.customfields.persistence.PersistenceFieldType;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.config.FieldConfig;

Sathish Venkat December 11, 2011

The jar which has these classes are a part of atlassian plugin Sdk.Download the atlassian plugin sdk set up the plugin development environment in your machine.This will help you have a good development environment for any jira component.

Also look for a jar file named jira-api-4.4.jar (change it according to your version) in the WEB-INF/lib of your jira installation.This jar also has all the above classes.

0 votes
Sathish Venkat November 17, 2011

Building a custom plugin would solve your problem.Below is the sample plugin code that will create a new custom field type of multiselect.

It automatically deletes the previous values and creates new options which will be components specific to that issue for each of the issue.(that was my requirement).

In the getValueFromIssue method change it according to your requirement and create the options.Those options will be stored for the field.Do that only if there is update required.This piece of code will be always called when ever a screen is opened which will have your customfield.

package com.test.new.jira.newcustomfields;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import com.atlassian.jira.bc.project.component.ProjectComponent;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.customfields.impl.MultiSelectCFType;
import com.atlassian.jira.issue.customfields.manager.GenericConfigManager;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.issue.customfields.persistence.CustomFieldValuePersister;
import com.atlassian.jira.issue.customfields.persistence.PersistenceFieldType;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.config.FieldConfig;


public class BuildCompSelectionField extends MultiSelectCFType{

private static Logger LOG = Logger.getLogger(BuildCompSelectionField.class);
public BuildCompSelectionField(OptionsManager optionsManager,
CustomFieldValuePersister valuePersister,
GenericConfigManager genericConfigManager) {
super(optionsManager, valuePersister, genericConfigManager);

}
@Override
public Object getValueFromIssue(CustomField field, Issue issue) {
List values = null;
try{
LOG.debug("In getValueFromIssue Method");
Collection<ProjectComponent> components = issue.getComponentObjects();
Iterator<ProjectComponent> compIter = components.iterator();
Collection<Option> compList = new ArrayList();
FieldConfig config = field.getRelevantConfig(issue);

Option newOption = null;
int i=0;
optionsManager.removeCustomFieldOptions(field);
while(compIter.hasNext()){
ProjectComponent component = (ProjectComponent)compIter.next();
LOG.debug("Going to create an option"+component.getName());
newOption = optionsManager.createOption(config, null, new Long(i), component.getName());
compList.add(newOption);
i++;
}
LOG.debug("Going to create the value");
field.createValue(issue, compList);
values = customFieldValuePersister.getValues(field, issue.getId(), PersistenceFieldType.TYPE_LIMITED_TEXT);
}catch(Exception e){
LOG.error("Error in custom field setting "+e.getMessage());
}
if(values == null || values.isEmpty())
return null;
else
return values;
}

@Override
public String getChangelogString(CustomField field, Object value) {
LOG.debug("Going to return empty value");
return "Test";
}

@Override
public String getChangelogValue(CustomField field, Object value) {
// TODO Auto-generated method stub
LOG.debug("return test in change log value");
return "Test";
}

@Override
public void updateValue(CustomField customField, Issue issue, Object value) {
//do nothing
LOG.debug("do nothing in updatevalue");
}

}

Regards

Sathish

Joshua Augustinus March 21, 2012

Hi Sathish, do you have the code for the velocity templates?

SRIDHAR June 17, 2013

Instead of components i want to get the list of values from my custom field Select List. How do i get the values

Collection<ProjectComponent> components = issue.getComponentObjects();

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events