convertToSingleSelect behaviour

Hi,

We use convertToSingleSelect in two ways:

1. With a static list of values set in the script of the REST endpoint that the converted field is linked to - useful when we need a hideable single select list.

2. With an endpoint that is connected to an external database - to get data that can be updated by users that are not Jira admins but have access to that external database.

In the first scenario this behaviour is working like this:

When you select an option from the list that option is no longer accessible if you want to change selected option. So with every option that you select, the list is becoming shorter and shorter. 

In the second scenario when you select an option you cannot change it at all because the list becomes empty. You need to reload whole form to see full list again.

Is it possible to configure such converted field to behave the same way as a normal single select list? So all values remain in the list no matter how many times a user changes his mind while reporting new ticket. 

Regards,

Bartek

1 answer

1 accepted

2 votes
Jenna Davis Community Champion Dec 05, 2017

Hello, 

For your first scenario, this should be possible. Could you please send me an example of the code you're using currently? I might be able to see where you need to make changes. 

For the second, you might take a look at this example: 

https://scriptrunner.adaptavist.com/latest/jira/behaviours-conversions.html#_walkthrough_choose_from_a_database_table

Or, there are several other select list conversion examples here:

https://scriptrunner.adaptavist.com/latest/jira/behaviours-conversions.html

Jenna

Hi Jenna,

In the first scenario we use following behaviour to convert a text field to single select list:

def score = getFieldById("customfield_16403")

score.convertToSingleSelect([
ajaxOptions: [
url : getBaseUrl() + "/rest/scriptrunner/latest/custom/scores",
query: false,
formatResponse: "general",
],
css: "max-width: 500px; width: 500px",
])

And a simple custom endpoint:

import groovy.json.JsonOutput
import groovy.transform.BaseScript
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

class pozycja {
def value
def label
}

scores () { MultivaluedMap queryParams, String body,HttpServletRequest request ->

def lista = ['1','2','3','4','5']
def result = []
def map = [:]

lista.each {
result << new pozycja(value:it, label:it)
}

map.put('items', result)

Response.ok(JsonOutput.toJson(map)).build()

As for the second scenario -  I have looked into your examples (without it I couldn't make it running in the first place;) ) but I haven't found anything to make it work exactly as a true single select list.

Regards,

Bartek

Hello, 

Here's a simple example of a REST endpoint that does something similar to what you're wanting (for your first question) :

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

doSomething() { MultivaluedMap queryParams ->

    def query = queryParams.getFirst("query") as String
    def fullList = [:]
    def originalList = ['A', 'B', 'C'] // test list, put your list here!    
def list = [] if(query){ def length = query.length() originalList.each{ if(it.substring(0, length) == query){ list.push(it) } } } else{list = originalList} fullList = [ items: list.collect { [ value: it, html : it, label: it, ] }, ] return Response.ok(new JsonBuilder(fullList).toString()).build() }

 And here's the behaviour that goes with it:

getFieldByName("TextFieldA").convertToSingleSelect([ 
    ajaxOptions: [
        url : getBaseUrl() + "/rest/scriptrunner/latest/custom/doSomething", 
        query: true, // keep going back to the sever for each keystroke        
minQueryLength: 1, keyInputPeriod: 500, formatResponse: "general", ] ])

My example probably needs some more intensive error handling added, so if something causes it to break keep that in mind. Hopefully that will help you out with your first question!

As far as querying the database, I'm not sure what you mean by making it a true single select. Can you send what you're doing currently?

Jenna

Wow, that is clever! Thanks a lot, Jenna! I will try to implement this approach also to our second scenario (with a database).

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
Asked 14m ago in Confluence

What are the resources that you use to learn more about Atlassian Products?

I am gathering information about resources available for Atlassian product knowledge transferring for a presentation in our local Atlassian User Group. I want to group them in four categories From ...

3 views 0 1
View question

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