Determining valid values for a custom field
Answered
Is there a way, in a custom workflow, to determine if a value for a field exists?
For example, something like:
var reportedVersion = {someWayToGetVersionProgrammatically};
var field = ctx.issue.project.findFieldByName('Affected versions');
if (field.hasValue(reportedVersion))
ctx.issue.project.fields['Affected versions'].add(reportedVersion);
else
ctx.issue.project.fields['Affected versions'].add('Unknown');
Setting a field to a version it doesn't understand throws an exception (which makes sense, of course), but it makes it difficult to do something like the above when you aren't guaranteed that the programmatic creation of the value will result in something valid.
Please sign in to leave a comment.
Hello, you can use the find() method for that: https://www.jetbrains.com/help/youtrack/incloud/v1-Set.html#methods
For example:
```
ctx.issue.project.fields['Affected versions'].values.find(function(val) {
return val.name === "1.0";
})
```
Sorry, I wasn't clear. I want to determine if a value that is about to be set is valid. For example, if there is a custom field of type enum defining "Unspecified", "Yes", "No", "True", "False", I would like to make sure a programmatically-created value of "Maybe" would be invalid so I can set the field to "Unspecified" instead.
EDIT: I realized that your code was looking at the fields on the project and that it might work. :)
EDIT 2: I tried the above code and got 'Cannot read property "values" from undefined'.
EDIT 3: I fixed the problem by doing the following and it worked correctly:
Thanks for the help. :)
Sorry to reopen this issue, but the above code seems to no longer work correctly (using YouTrack InCloud 2017.4 Build 38030). Here is the code (crashing when setting the value):
Any ideas?
It is expected in API that you add a field value to the set of values, not a string representation of it. Adding a string representation may have worked, but it is more of a hidden feature (and may be removed any moment) than a part of API.
So, I would suggest updating your `versionStrDefinied` function to return the value you find instead of just comparing it to `null`, and use this value:
Adding a string representation may have worked, but it is more of a hidden feature (and may be removed any moment) than a part of API.
That's good to know.
So, I would suggest updating your `versionStrDefinied` function to return the value you find instead of just comparing it to `null`, and use this value
Switching to using the value found in the field fixed my issue. Thanks!