Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

ScriptRunner Behaviour setFieldOptions Not Working.

carlos andrade May 11, 2024

I have 3 Select List (single choice) custom fields. I want to set the available options of the fields in 2 & 3 depending on the selection of field 1. To test this, in the Initialize script of the behaviour I am able to set debug messages on the field 2 and 3 using setHelpText, but when I use setFieldOptions, the available options do not change. Any ideas?


// Define custom field IDs
String level1FieldId = "customfield_16728"
String level2FieldId = "customfield_16729"
String level3FieldId = "customfield_16730"

// Retrieve the fields by their IDs
def level1Field = getFieldById(level1FieldId)
def level2Field = getFieldById(level2FieldId)
def level3Field = getFieldById(level3FieldId)

level2Field.setHelpText("Initialize Level2Field")
level3Field.setHelpText("Initialize Level3Field")
level2Field.setFieldOptions([
    [value: "Test Field 2", description: "Test Option 1"]
    ])
level3Field.setFieldOptions([
    [value: "Test Filed 3", description: "Test Option 1"]
    ])

4 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
carlos andrade May 13, 2024

Thank you @Fadoua and @Ram Kumar Aravindakshan _Adaptavist_  I found the problem. I didn't realized that all the possible options for fields 2 and 3 had to exist for it to work. I am sorry about that.

Once I set all the defaults possible in fields 2 and 3, it worked.

For example:   

    'Unresponsive to User Inputs',
    'Erratic Behavior',
    'Incorrect Behavior',
    'Not Detected by Host PC'
    'No Video Signal',
    'Intermittent Video',
    'Incorrect Resolution',  
    'Video Signal Present - No Image',  
    'Image not Updating',    
    'Physical Damage' , etc.
Thank you again for your help. I learned a lot. 
Fadoua
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 13, 2024

Believe me I was about to ask you if you had the options already added or are you trying to add them through the script.

Glad it worked at the end! Yes ScriptRunner is very vast and there is a lot to learn in it.

If you have any questions please don't hesitate to ask

Best of luck!

Fadoua

Like # people like this
2 votes
Fadoua
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 11, 2024

@carlos andrade 

Your code shouldn't be in the initializer field, it should be in the field itself.

Check the following code as I tried it myself and it worked without issues:

https://library.adaptavist.com/entity/dynamic-select

Let me know if you have any questions.

Best,

Fadoua

carlos andrade May 11, 2024

Thanks! I did triy it in the field itself too with the same result. I'll check that web page.

Fadoua
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 11, 2024

@carlos andrade 

You will have to add a server script for both field 2 and 3 not in field 1

carlos andrade May 12, 2024

Thank you! When I create Fields 2 and 3 should I set some defaults for those?

They are still not being updated with the new options from the script, even though it is going through the right sections of the script according to debug messages..

Fadoua
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 12, 2024

@carlos andrade 

Can you please share the script for each field exactly the way you have it?

Best,

Fadoua

carlos andrade May 12, 2024

Here it is. Right now I am just trying it in jusr field 2

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

final singleSelectName = 'Failed System'
final selectName = 'Failed System Symtom'

def cfManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager

def singleSelectField = cfManager.getCustomFieldObjectsByName(selectName)[0]
def formSingleSelect = getFieldByName(singleSelectName)
def singleSelectValue = formSingleSelect?.value
def formSelect = getFieldByName(selectName)

// Make sure the fields we want to work with are on the form
if (!formSingleSelect && !formSelect) {
    return
}

// Must grab the relevant config given the current issue context and the custom field
def config = ComponentAccessor.fieldConfigSchemeManager.getRelevantConfig(issueContext, singleSelectField)
// Given config, grab a map of the possible options for the second select custom field
def options = optionsManager.getOptions(config)

// Define your preset options for each single select case using the format shown here.
// Just put the values, not the keys/IDs.
def optionSet1 = [
    'Unresponsive to User Inputs',
    'Erratic Behavior',
    'Incorrect Behavior',
    'Not Detected by Host PC'
]
def optionSet2 = [
    'No Video Signal',
    'Intermittent Video',
    'Incorrect Resolution',  
    'Video Signal Present - No Image',  
    'Image not Updating',    
    'Physical Damage'
]
def optionSet3 = [
    'Fails To Power On',
    'Fails To Boot'
]

// Make sure the second field actually has options to set
if (!options) {
        formSelect.setHelpText("No options")
    return
} else {
    formSelect.setHelpText("Options" + options)
    return
}

// Set/use the appropriate optionSet, dependent on the value of the currently selected single select option
formSingleSelect.setHelpText(" loop" + singleSelectValue)
switch (singleSelectValue){
// Notice: The optionSet that is used is changed in each case
// Change 'Single Select Option...' to match your single select's values.
    case 'Control (I/O) Device':
        formSelect.setFieldOptions(options.findAll { it.value in optionSet1 }.collectEntries {
            [(it.optionId): it.value]
        })
        formSelect.setHelpText("option 1")
        break
    case 'Video':
        formSelect.setFieldOptions(options.findAll { it.value in optionSet2 }.collectEntries {
            [(it.optionId): it.value]
        })
           formSelect.setHelpText("option 2")
        break
    case 'Computer / Network Switch':
        formSelect.setFieldOptions(options.findAll { it.value in optionSet3 }.collectEntries {
            [(it.optionId): it.value]
        })
           formSelect.setHelpText("option 3")
        break
// Reset to default options if single select option is null or any other option that is not taken care of
    default:
       formSelect.setHelpText("Default")
        formSelect.setFieldOptions(options)
}
1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 13, 2024

Hi @carlos andrade

Setting the field options is very straightforward. You only need to pass a list value to it. 

However, in your code, you appeared to have passed a Map, hence the issue.

Please modify your code accordingly to something like this:-

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

@BaseScript FieldBehaviours fieldBehaviours
def list1 = getFieldById(fieldChanged)
def list1Value = list1.value.toString()

def list2 = getFieldByName('List2')
def list3 = getFieldByName('List3')


def list2NewOptions = ['Option1', 'Option2', 'Option3', 'Option4', 'Option5']

def list3NewOptions = ['Choice1', 'Choice2', 'Choice3', 'Choice4', 'Choice5']


if(list1Value == 'Zone1') {

list2NewOptions = ['Option1', 'Option2']
list3NewOptions = ['Choice1, 'Choice2']

} else if(list1Value == 'Zone2') {

list2NewOptions = ['Option3', 'Option4']
list3NewOptions = ['Choice3, 'Choice4']

} else if(list1Value == "Zone3") {

list2NewOptions = ['Option5']
list3NewOptions = ['Choice5']
}


list2.setFieldOptions(list2NewOptions)

list3.setFieldOptions(list3NewOptions)

Please note that the sample working code above is not 100% exact to your environment. Hence you will need to make the required modifications.

Below is a screenshot of the Server-Side Behaviour configuration:-

behaviour_config.png

In the example code above, the Server-Side Behaviour is configured for List1. In your case, you must configure a Server-Side Behaviour for the Leve 1 field.

Also, please note, when configuring a Server-Side Behaviour, the field the Server-Side Behaviour is added for must be declared as:-

def leve1Field = getFieldById(fieldChanged)

The fieldChanged option must be used to ensure that the Behaviour correctly detects when a change has been made to a particular field and triggers the Behaviour accordingly.

I am also including a couple of test screenshots for your reference:-

1. On the create screen, when an option is selected from List1 as shown in the screenshot below, the options in List2 and List3 are also filtered accordingly in the following screenshots

test1.png

test2.png

test3.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

carlos andrade May 13, 2024

Thanks for replying, Ram. I am still having the same problem. It looks like the code is working as it's supposed to, but the new lists are not being displayed.  Here is your script with my modifications: 

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

@BaseScript FieldBehaviours fieldBehaviours
def list1 = getFieldById(fieldChanged)
def list1Value = list1.value.toString()

def list2 = getFieldByName('Failed System Symtom')
def list3 = getFieldByName('Failed System Cause')


def list2NewOptions = ['Option1', 'Option2', 'Option3', 'Option4']

def list3NewOptions = ['Choice1', 'Choice2', 'Choice3', 'Choice4']


if(list1Value == 'Control (I/O) Device') {
    list1.setHelpText("Control (I/O) Device")
    list2NewOptions = ['Unresponsive to User Inputs', 'Erratic Behavior','Incorrect Behavior','Not Detected by Host PC']
    list3NewOptions = ['Unplugged Cable', 'Faulty Cable','Damaged Cable','Faulty Adapter']
 
} else if(list1Value == 'Video') {
    list1.setHelpText("Video")
    list2NewOptions = ['No Video Signal', 'Intermittent Video','Incorrect Resolution','Video Signal Present - No Image']
    list3NewOptions = ['Unplugged Cable', 'Faulty Cable','Damaged Cable','Faulty Adapter']

} else if(list1Value == "Computer / Network Switch") {
    list1.setHelpText("Computer / Network Switch")
    list2NewOptions = ['Fails To Power On','Fails To Boot','Slow To Boot','Fails POST']
    list3NewOptions = ['Faulty Drive','Faulty Motherboard','Failed Memory','Faulty Power Supply']
} else {
        list1.setHelpText("No Zone" + list1Value)
}
list2.setFieldOptions(list2NewOptions)
list2.setHelpText("List 2 Options" + list2NewOptions)

list3.setFieldOptions(list3NewOptions)
list3.setHelpText("List 3 Options" + list3NewOptions)
Failed System Output.png
Like Dave Rosenlund likes this
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 13, 2024

Hi @carlos andrade

Please clarify what version of Jira and ScriptRunner runner you are currently using.

 

Thank you and Kind regards,

Ram

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 13, 2024

Hi @carlos andrade

Great to hear the solution worked. :-)

Please accept the answer.

Thank you and Kind regards,

Ram

0 votes
carlos andrade May 13, 2024

@Ram Kumar Aravindakshan _Adaptavist_ I am using Jira Software 9.12.1 and ScriptRunner 8.21.0 but I see there is an update for version 8.27.0. I will update to to this latest version.

TAGS
AUG Leaders

Atlassian Community Events