[RESOLVED] How to check if a value exists in an enum list?
Hello,
I have an enum list, it's very large, is there any way to check if there is a value in the list other than looping through all the values in the list? As in this example:
issue.fields.NameOfYourField.forEach((v)=>{
console.log(v.name);
})
I tried something like this but it doesn't work:
const x = ctx.project.findFieldByName(ctx.UserEnum.name).findValueByName('username');
That doesn't work either:
const x = ctx.issue.project.findFieldByName('User').has('username');
I just need to know if the assignment was successful:
ctx.issue.fields.User = ‘username’;
I also tried using try/catch but the error is not returned if ‘username’ does not exist in the list.
Please sign in to leave a comment.
Hi Viacheslav Bachynskyi,
You can use either the has method or the find method of the
values
set as long as the field is defined in the requirements section:If the field is of the
user
type, you need to assign a user object to it instead of a login string:Catching the error should be possible with try/catch:
However, if it's not returned for some reason, you can just check the field value after the assignment to see if it's what you expect.
Hi. Sorry for misleading you, the field type is not User, but Enum.
Hi.
Sorry, my mistake, the try {} catch (e) {} block works correctly, I put the } in the wrong place in the code. This check is quite enough. Thank you.
Hi,
You did state that you work with an enum list, so the misunderstanding is on me. I'm glad that the try-catch block works, but please note though that you can also use the find method with an enum field: