Make a field writable only if a certain value is set in another field
Imagine a scenario where you want to get the user’s favourite fruit:
The problem here is that our select list options probably don’t contain the names of all fruits, and there will always be a difficult person who wants something not on the list. What we’ll do is have an option Other, which if selected, will make a free text field appear.
Or more realistically, this field could be the names of your Customers, and you might want to allow people to add additional ones.
We’ll create a text field called Favourite Fruit (Other), which only becomes visible if the Other select list option is chosen. The, code to apply to the Favourite Fruit field, is:
And the result:
The behaviour is attached to the field to which we want to list for changes, in this case Favourite Fruit, and not the Favourite Fruit (Other) field.
This example makes use of
getFieldChanged(), which always returns the field ID of the field the behaviour fires for.
We can do a little bit more here for bonus points… we would like to create an
Option with the other value, then update the issue to have this new option, and remove the value from the other field. To do this we use a listener on the
Issue Updated event with the following code:
This script is just a starting point - you may want to check that the user-provided other value doesn’t actually already exist in the list.
As we used the custom field name in two different scripts it’s a good idea to extract it to some common class of constants, so you only need modify it in one place if you change the field display name.