Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,367,921
Community Members
 
Community Events
168
Community Groups

Solved: Can't read a custom field value from inside a Behaviour Initializer

Edited

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:

  1. "Select Option" is a single choice Select Field
  2. "Drop Down List" is a single line Text Field which i am overriding with a behaviour & Rest Endpoint combo:

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:

  • The same if statement that returns null in the initializer works fine in the server-side script (the field remains hidden until a 'Select  Option' is selected).
  • I have tried to 'save' the 'Select Option' then editing with the same result. 
  • All three Rest API Endpoints are almost the same (but return different content). Here's doError as an example:
  • 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()
    }
  • doError output:
  • {"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

2 answers

1 accepted

1 vote
Answer accepted

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.

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events