Sort BulkImportCustomFieldValues

I've implemented the BulkImportCustomFieldValues example using PHP and cURL, however, the values get added at the end of the existing option list, rather than in their respective alphabetical positions in the list. I haven't found any documentation on how to programmatically sort the option list after using my BulkImportCustomFieldValues script. What would you suggest?

1 answer

I figured I could create a custom REST endpoint to a script that sorts the specified field's options. However, I'm stuck trying to figure out how to get the FieldConfig object so that I can get the field options and sort them. Here's what I have so far. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.customfields.option.OptionsImpl
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import groovy.transform.BaseScript
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

import org.apache.log4j.Logger
import org.apache.log4j.Level
def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

@BaseScript CustomEndpointDelegate delegate

sortOptions(
httpMethod: "POST", groups: ["jira-administrators"]
) { MultivaluedMap queryParams, String body ->
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
log.debug("request body: " + body)
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(body)
log.debug("request object: " + object)
def field = customFieldManager.getCustomFieldObject((Long)object.field.toInteger())
log.debug("field: " + field)
field.getOptions(null, ...)
//use OptionsImpl sort function to sort the options

return Response.ok(new JsonBuilder(field).toString()).build()
}

 Any help would be appreciated.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

59 views 0 5
Read article

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