Workflow. Get value of enum custom field

Hi there. I created a custom enum field (multi) and created worflow, which auto create subtask. So I need get one value from my enum field.
I find that this statement ctx.issue.fields.locales returns data with this format: 

[{"$$type": " EnumField", "name": "UK"}, 
{"$$type": " EnumField", "name": "HU"},
{"$$type": " EnumField", "name": "RO"}]

How can I get separately RO or UK or HU in my example?

code of my workflow:


/**
* This is a template for an on-change rule. This rule defines what
* happens when a change is applied to an issue.
*
* For details, read the Quick Start Guide:
* https://www.jetbrains.com/help/youtrack/incloud/2022.1/Quick-Start-Guide-Workflows-JS.html
*/

const entities = require('@jetbrains/youtrack-scripting-api/entities');

exports.rule = entities.Issue.onChange({
// TODO: give the rule a human-readable title
title: 'Create_task_english_text',
guard: (ctx) => {
var issue = ctx.issue;
return ctx.issue.isReported && issue.fields.Type.name === ctx.Type.Localisation.name && issue.fields.Text.name === ctx.Text.english.name && issue.summary === 'test';
},
action: (ctx) => {
var issue = ctx.issue;
var newProject = entities.Project.findByName('New-Landings');








var createIssue = function() {


var newIssue = new entities.Issue(ctx.currentUser, newProject, issue.summary);


newIssue.description = ctx.issue.fields.locales + '\n';
newIssue.IssueAttachment = issue.IssueAttachment;

newIssue.applyCommand ('add Board ' + 'New projects', ctx.currentUser);
newIssue.applyCommand('work-type ' + 'Translation', ctx.currentUser);


newIssue.applyCommand ('Assignee ' + 'r.torshin', ctx.currentUser);
newIssue.links['subtask of'].add(issue);

};



createIssue();

},
requirements: {
Type: {
type: entities.EnumField.fieldType,
Localisation: {
name: 'localization'
},
New_landing: {
name: 'new landing'
}
},
Text: {
type: entities.EnumField.fieldType,
russian: {
name: 'only Russian text'
},
english:{
name: 'English text'
},
original:{
name: 'Original text'
},
},


}
});

0
3 comments

Hi!

I will copy my answer from your support request here in case other users have the same question.

Since your field is a multi-value one, you need to use the forEach to get all values in it:

ctx.issue.fields.locales.forEach(function(value) {
console.log(value.name);
});

Of course, change the console.log to the needed action.

Please do not forget to add the locales field to the requirements section:

locales: {
 name: "locales",
 type: entities.EnumField.fieldType,
multi: true
         }
1

what if multi is false. 

0

Please refer to our documentation that shows examples for both single and multi-value fields: Using the Workflow API. 

0

Please sign in to leave a comment.