Set value of select list conversion based on another select list conversion

Irtiza Rizvi March 3, 2017

Hello,

I have two text fields (say Address and PhoneNumber) and on both of them, I have setup a select list conversion behavior.  They both query from the same SQL DB table, just one searches for customer records by address and the other by phone number.  Here is the initializer script for one of them:

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

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


The REST endpoint in both cases returns a list of customer records where the label is just the phone number or address, respectively, the value is a string representation of the entire SQL row, and the HTML is data from the row displayed to the user in a table.

Now, when I select a customer record in the Address field, I also want to automatically set the PhoneNumber field to the phone number of that customer, and vice versa.  I tried to accomplish this using a server-side script, as follows:

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

def stringToMap = { String str ->
    return str[1..-2]
        .split(', ')
        .collectEntries { entry ->
            def pair = entry.split(':')
            [(pair.first()): pair.last()]
        }
}

def phoneField = getFieldById("customfield_11308")
def addressField = getFieldById("customfield_12003")

def value = stringToMap((String)addressField.getValue())

phoneField.setFieldOptions(["value": addressField.getValue(), "label": value.phonenumber, "html": ""])


I also tried phoneField.setFormValue(...), but that doesn't work either.  Also, if there's a better way of doing this, please let me know.

1 answer

0 votes
Irtiza Rizvi March 7, 2017

On a side note, when I have both behaviors (select list conversion A trying to update B and vice versa), I noticed I can't edit or create issues.  I looked at the browser console and saw the following JS error pop up a few times:

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
at Class.getDescriptor (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:1630)
at Class._setDescriptorWithValue (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:3389)
at HTMLSelectElement.<anonymous> (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:3389)
at HTMLSelectElement.dispatch (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:104)
at HTMLSelectElement.h (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:96)
at Object.trigger (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:101)
at HTMLSelectElement.<anonymous> (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:117)
at Function.each (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:54)
at init.each (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:47)
at init.trigger (batch.js?atlassian.aui.raphael.disabled=true&locale=en-US:117)

My guess is that the JS error hoses the edit/create form, which prevents the button from submitting.

Suggest an answer

Log in or Sign up to answer