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

0
4 comments
Official 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:

ctx.User.values.has(entities.User.findByLogin("username")); // Returns true or false
ctx.User.values.find(u => u.login === "username"); // Returns first found object or undefined

If the field is of the user type, you need to assign a user object to it instead of a login string:

ctx.issue.fields.User = entities.User.findByLogin("username");

Catching the error should be possible with try/catch:

try {
	ctx.issue.fields.User = entities.User.findByLogin("username");
}
catch (e) {
	console.log(String(e));
}

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.

0

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.

0

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:

ctx.Enum.values.find(u => u.name === "name-of-value");
0

Please sign in to leave a comment.