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? 

1
13 comments
Official 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:

var GenerateTransitions = function(TypeValuesMapping, TypeValue) {
var targers = {};
TypeValuesMapping[TypeValue].forEach(function(value) {
var target = {
targetState: value
};
targers[value] = target;
});
return targers;
};

var GenerateStateMachine = function(TypeValuesMapping, TypeValue) {
var requiredValues = TypeValuesMapping[TypeValue];
var stateMachine = {};
var needSetInitial = true;
requiredValues.forEach(function(elem) {
var item = {};
item.transitions = GenerateTransitions(TypeValuesMapping, TypeValue);
if (needSetInitial) {
item.initial = true;
needSetInitial = false;
}
stateMachine[elem] = item;
});
return stateMachine;
};
var GenerateAlternativeMachines = function(TypeValuesMapping) {
var alternativeMachines = {};
for (var typeValue in TypeValuesMapping) {
alternativeMachines[typeValue] = GenerateStateMachine(TypeValuesMapping, typeValue);
}
return alternativeMachines;
};
exports.GenerateStateMachine = GenerateStateMachine;
exports.GenerateAlternativeMachines = GenerateAlternativeMachines;

Then, create a separate State-machines per Issue Type rule and add a reference for transitions-helper:

var helper =  require('./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

var cities = {
'Czech Republic': ['Prague'],
Netherlands: ['Amsterdam', 'Rotterdam'],
Germany: ['Berlin', 'Hamburg', 'Munich'],
Russia: ['Moscow', 'Saint Petersburg'],
USA: ['New York', 'Los Angeles', 'Chicago']
};

Then, specify the stateFieldName and typeFieldName properties:

stateFieldName: 'City',
typeFieldName: 'Country',

Now, set the default state machine using the GenerateStateMachine method from our helper:

defaultMachine: helper.GenerateStateMachine(cities, 'Russia'),

And finally, add alternative state machines:

alternativeMachines: helper.GenerateAlternativeMachines(cities),

Here is the complete code:

var entities = require('@jetbrains/youtrack-scripting-api/entities');
var helper = require('./transitions-helper');

var cities = {
'Czech Republic': ['Prague'],
Netherlands: ['Amsterdam', 'Rotterdam'],
Germany: ['Berlin', 'Hamburg', 'Munich'],
Russia: ['Moscow', 'Saint Petersburg'],
USA: ['New York', 'Los Angeles', 'Chicago']
};

exports.rule = entities.Issue.stateMachine({
title: 'Cities per Country',
stateFieldName: 'City',
typeFieldName: 'Country',
defaultMachine: helper.GenerateStateMachine(cities, 'Russia'),
alternativeMachines: helper.GenerateAlternativeMachines(cities),
requirements: {}
});

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:

0

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. 

 

1

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.

0

Hi Kyriakos Pavlidis!

You can store this data as field values, so you can retrieve them both via the REST and workflow API.

So on setup of project we can save this object even send it through API.

You can create a custom project template, which, among other things, lets you quickly set up predefined fields with values.

0

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?

0

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>>

0

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. 

0

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?

0

My question is about storing this data in a field or data in this format.

 

var cities = {
'Czech Republic': ['Prague'],
Netherlands: ['Amsterdam', 'Rotterdam'],
Germany: ['Berlin', 'Hamburg', 'Munich'],
Russia: ['Moscow', 'Saint Petersburg'],
USA: ['New York', 'Los Angeles', 'Chicago']
};
0

It's not possible to store this data in a single field, hence the whole workaround described by Oleg above. 

0

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.

1

Hello Alisa Kasyanova  thank you for your answer 

0

Please sign in to leave a comment.