Getting current text from field in Select List conversion behavior

Irtiza Rizvi November 6, 2016

Hello,

I need to create the ability to lookup records by phone number from an external database.  My current setup is as follows:

1) A REST Endpoint connects to the external database and performs a SQL query given a phone number

2) A select list conversion displays the results to the user as they type a phone number into a text field

My question is how do I get the current text in the text field.  For example, if the user types "123-45" into the text field, but has nothing selected, then I want to grab the text "123-45" so I can send it to the REST endpoint to perform the query.

I tried the following code, but it always returns null:

def formField = getFieldById(getFieldChanged());
String phoneNum = formField.getValue();

2 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Irtiza Rizvi November 10, 2016

I was able to solve the issue by looking at the documentation for behaviors.  The current text is stored in a GET parameter called "query" so you can access it through the REST endpoint, as follows:

def query = queryParams.getFirst("query") as String

 

 

 

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 10, 2016

Awesome... I didn't understand your issue related to the rest endpoint, but yes, that's how to get the params.

Irtiza Rizvi November 11, 2016

No worries, I thought I would need to send the query as a GET param to the REST endpoint myself and was fixated on the behavior as a result.  But it turns out that the "query" param is there automatically, and I didn't have to do anything at all!

0 votes
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 10, 2016

It's only going to have a value when one of the valid options is set.

Getting the current text should all be done when you configure the select list conversion - that's where you specify the rest endpoint. What's your code for that?

Irtiza Rizvi November 10, 2016

Hi Jamie,

Here is the code for the initializer function for my behavior:

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
  
@BaseScript FieldBehaviours fieldBehaviours

def formField = getFieldById(getFieldChanged());
String phoneNum = formField.getValue();

getFieldById("customfield_11308").convertToSingleSelect([
    ajaxOptions: [
        url: getBaseUrl() + "/rest/scriptrunner/latest/custom/customers?phone=$phoneNum",
        query: true,
        formatResponse: "general"
    ],
    css: "max-width: 500px; width: 500px",
]);

Here is the rest of the configuration:

image2016-11-3 5-24-50.png

TAGS
AUG Leaders

Atlassian Community Events