This exercise is the continuation of:

In this case, instead of use an external REST API to obtain the values of the dropdown, we want to use a static list of values. We will use the same approach than the other exercise.

First we will create two standard custom fields in Jira of type “Text Single Line“, in this case, with the name the first one “Website” and the other one “Website to exclude

We will create a Behaviour (in the scope/context of the new customfields project/issuetypes)


import com.atlassian.jira.component.ComponentAccessor
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

getFieldByName("Website").convertToMultiSelect([
ajaxOptions: [
url : getBaseUrl() + "/rest/scriptrunner/latest/custom/FieldQuery?fieldname=Website",
query: true, // keep going back to the sever for each keystroke
minQueryLength: 2,
keyInputPeriod: 500,
formatResponse: "general",
]
])

getFieldByName("Website to exclude").convertToMultiSelect([
ajaxOptions: [
url : getBaseUrl() + "/rest/scriptrunner/latest/custom/FieldQuery?fieldname=Exclude",
query: true, // keep going back to the sever for each keystroke
minQueryLength: 2,
keyInputPeriod: 500,
formatResponse: "general",
]
])

After the creation of the Behaviour, we need to continue creating the REST custom endpoint in our Jira Server.

With this code (to create the REST method FieldQuery with one input param):


import groovy.json.JsonSlurper; 
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.component.ComponentAccessor

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

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

@BaseScript CustomEndpointDelegate delegate

FieldQuery(httpMethod: "GET") { MultivaluedMap queryParams ->

def query = queryParams.getFirst("query") as String
def fieldname = queryParams.getFirst("fieldname") as String
if (!query) query =""

def rt = [:]
def retorno = ""
if (query || true ) {

def items = []
if (fieldname == "Website")
items = ['ALL','WEBA','WEBB','...']
if (fieldname == "Exclude")
items = ['ALL','WEBA','WEBB','...']
def item_list_init = '{"items":[';

int i = 0;
int j = 0;
while ( i < items.size()) {
if (items[i].toLowerCase().contains(query.toLowerCase())|| query == "") {
item_list_init = item_list_init + '{"value":"'+items[i]+'","html":"'+items[i]+'","label":"'+items[i]+'"},'
j++;
}
i++; 
}
def item_list_end = '],"total":"' + j + '","footer":"Choose..."}';
//return item_list_init[0..-2] + item_list_end;
retorno = item_list_init[0..-2] + item_list_end;
if (j == 0) {
retorno = '{"items":[],"total":"0","footer":"Choose..."}'
}

/*rt = [
items: seleccion.Resources*.displayableName.collect { def app ->
[
value: app,
html : app,
label: app
]
},
total: seleccion.totalResults,
footer: "Choose app..."
]*/
}

//return Response.ok(new JsonBuilder(rt).toString()).build();
return Response.ok(retorno).build();
}

Once finished, you can try to use it! That’s all! It’s predictable and very nice to use this kind of fields instead of the usual horrible dropdowns multivalue of the standard Jira

And…

Note that is possible to create multiple cascade type fields in this way, and it’s simple, only you need to send a new parameter and use new Behaviours to detect the change of the fields and change the list values of the dropdown. We will see in next exercises

By MrAddon

Posted by:.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s