You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
Hi,
I wish to populate the drop down list with values based on a previously selected option.
For that purpose I configured the two custom fields:
Behaviour initializer:
def option = getFieldByName("Select Option")
def selected_option = option.getValue() as String
def this_url = "/rest/scriptrunner/latest/custom/doSomething"
log.error("Checking selected project: $option")
if (selected_option) {
log.error("Selected option: $selected_option")
if (selected_option == 'option1'){
this_url = "/rest/scriptrunner/latest/custom/doOptionA"
log.error("Setting this_url: $this_url")
} else if (selected_option == 'option2'){
this_url = "/rest/scriptrunner/latest/custom/doOptionB"
log.error("Setting this_url: $this_url")
} else {
log.error("Unknown select option $selected_option")
this_url = "/rest/scriptrunner/latest/custom/doError"
}
} else {
log.error("Error: Selected option is null.")
this_url = "/rest/scriptrunner/latest/custom/doError"
log.error("Setting this_url: $this_url")
}
log.error("this_url: $this_url")
getFieldByName("Drop Down List").convertToSingleSelect([
ajaxOptions: [
url : getBaseUrl() + this_url,
query: true, // keep going back to the sever for each keystroke
minQueryLength: 1,
keyInputPeriod: 500,
formatResponse: "general",
]
])
Server-side script associated to: "Drop Down List":
def drop_down_list_field = getFieldByName("Drop Down List")
def project = getFieldByName("Select Option")
def selected_project = project.getValue() as String
if (selected_project == 'option1'){
drop_down_list_field.setHidden(false)
} else if (selected_project == 'option2'){
drop_down_list_field.setHidden(false)
} else {
drop_down_list_field.setHidden(true)
}
Unfortunately, this the initializer code keeps failing back the 2nd else clause:
[c.o.j.groovy.user.FieldBehaviours] Checking selected project: Form field ID: customfield_10822, value: null
[c.o.j.groovy.user.FieldBehaviours] Error: Selected option is null.
[c.o.j.groovy.user.FieldBehaviours] Setting this_url: /rest/scriptrunner/latest/custom/doError
[c.o.j.groovy.user.FieldBehaviours] this_url: /rest/scriptrunner/latest/custom/doError
A few notes:
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
doError() { MultivaluedMap queryParams ->
def query = queryParams.getFirst("query") as String
def fullList = [:]
def originalList = ['Error-1', 'Error-2', 'Error-3'] // test list, put your list here!
def list = []
if(query){
def length = query.length()
originalList.each{
if(it.toString().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()
}
{"items":[{"value":"Error-1","html":"Error-1","label":"Error-1"},{"value":"Error-2","html":"Error-2","label":"Error-2"},{"value":"Error-3","html":"Error-3","label":"Error-3"}]}
Any help would be greatly appreciated.
Br,
Yoav
Solved my own question. For this use case there is no use for a Behaviour initializer. the entire code goes in the server-side script as such:
def drop_down_list_field = getFieldByName("Drop Down List")
drop_down_list_field.setHidden(true)
def selected_option = getFieldById(getFieldChanged()).value as String
def this_url = "/rest/scriptrunner/latest/custom/doSomething"
if (selected_option) {
drop_down_list_field.setHidden(false)
if (selected_option == 'option1'){
this_url = "/rest/scriptrunner/latest/custom/doOptionA"
} else if (selected_option == 'option2'){
this_url = "/rest/scriptrunner/latest/custom/doOptionB"
} else {
this_url = "/rest/scriptrunner/latest/custom/doError"
}
} else {
this_url = "/rest/scriptrunner/latest/custom/doError"
}
log.error("this_url: $this_url")
getFieldByName("Drop Down List").convertToSingleSelect([
ajaxOptions: [
url : getBaseUrl() + this_url,
query: true, // keep going back to the sever for each keystroke
minQueryLength: 1,
keyInputPeriod: 500,
formatResponse: "general",
]
])
Definitely the initialiser looked like the thing you needed to check.
Thank you for sharing your own answer. If you want, you can answer yourself and accept it as an accepted answer, maybe it can help others.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.