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? 

3 comments
Comment actions Permalink
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! 

Comment actions Permalink

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
Comment actions Permalink

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

Please sign in to leave a comment.