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.

0
5 comments

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";
})

```

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:

var affectedVersionsField = issue.project.fields.find(function(f) { return f.name === 'Affected versions'; });

Thanks for the help. :)

0

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

action: function(ctx) {
var issue = ctx.issue;

var versionStr = findVersion(issue.description);
if (versionStrDefinied(issue.project, versionStr))
{
issue.fields['Affected versions'].add(versionStr); // Crash here - value doesn't exist
issue.fields['Affected versions'].delete('Unknown');
}
}

// Determines if the version string for the Affected Version field is a defined one
function versionStrDefinied(project, versionStr) {
if (!versionStr)
return false;

var afectedVersionsField = project.fields.find(function(f) { return f.name === 'Affected versions'; });
if (!afectedVersionsField)
return false;

return afectedVersionsField.values.find(function(v) { return v.name === versionStr; }) !== null;
}

Any ideas?

 

0
Avatar
Permanently deleted user

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:  

issue.fields['Affected versions'].add(version);
0

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!

0

Please sign in to leave a comment.