How to filter custom field values based on the selection of another field
Answered
Let's assume that I have two custom fields: Country and City. I need to show cities only for the corresponding selected country:
How to implement this?
Please sign in to leave a comment.
First of all, let's create a field Country with its values:
and City:
As you can see, by default, all cities are shown independently of the selected country:
To filter these values, you can use the State-machines per Issue Type workflow rule. The example сode may seem complicated, but do not worry, we will try to simplify it.
For this, create a separate custom script and call it, for example, transitions-helper:
Then, create a separate State-machines per Issue Type rule and add a reference for transitions-helper:
Create a list of cities that should show for each country. Please note that all values should be the same as values in fields Country and City:
Then, specify the stateFieldName and typeFieldName properties:
Now, set the default state machine using the GenerateStateMachine method from our helper:
And finally, add alternative state machines:
Here is the complete code:
I hope it will be helpful!
Your example worked very well Oleg, thank you.
I have one issue though - After implementing this workflow, the "city" fieldvalues are all lowercase, and their colors aren't visible.
With workflow:
Without workflow:
Hello,
Please accept my apologies for the delayed response. I see that you created a separate State Machine per Issue type workflow forces lowercase thread, so let's continue it's discussing there.
Hi Oleg Larshin thank you for this solution. Is it possible to make the cities object more dynamic. What do i mean imagine 40 projects that they have these two field with different combination.
So is it possible to store this object somewhere in the project in a variable that we can call in the workflow. So on setup of project we can save this object even send it through API.
Hi Kyriakos Pavlidis!
You can store this data as field values, so you can retrieve them both via the REST and workflow API.
You can create a custom project template, which, among other things, lets you quickly set up predefined fields with values.
Dear Sergey thank you for this I am somehow worried, is it possible to give me an example on how to save it in custom field what type will be the custom field set? Based on another topic about potentially having this type of map> field your answer was negative https://youtrack-support.jetbrains.com/hc/en-us/community/posts/12308891346322-Map-object-as-custom-field what is the case?
Dear Sergey Merzlov can you give an example of how to store this data in field values cause the type of data is technically Map<String,List<String>>
Kyriakos Pavlidis my reply here was about storing data in a field, accessing it via APIs, and reusing it in project creation. If your initial question here was in fact the same as in https://youtrack-support.jetbrains.com/hc/en-us/community/posts/12308891346322-Map-object-as-custom-field, then the answer there still stands.
So saving an object is not an option based on the other post. And then what do you mean about storing data in a field then. What type of field would this be a text field and then use a parser to get the json object?
My question is about storing this data in a field or data in this format.
It's not possible to store this data in a single field, hence the whole workaround described by Oleg above.
Hello Osman
Unfortunately, it is a known bug about SLA being in conflict with state machine workflows. Please feel free to vote for it to receive a notification when it is fixed.
Hello Alisa Kasyanova thank you for your answer