JavaScript Workflows - access project enum fields

Hello. I have a standalone YouTrack 2017.2 server, working as a HelpDesk. On issue creation I would like to set up some of its fields automatically, so support engineer would have much easier life. Right now I am struggling with "Affected version" field. I have defined set of project versions with release dates, archived and released flags in the project properties. On issue registration, workflow should scan this list, find versions that has been released, but were not archived and assign them to the issue's affected versions field.

Right now I managed to create this workflow script for the task:

var versions = issue.fields['Affected versions'];     
      versions.forEach(function(v){
        if(!v.isArchived && v.isReleased )
          {
            issue.fields['Affected versions'].add(v);
          }

This however is not right. Issue by default has an empty set of affected versions, so loop would never execute. I have tried var versions = issue.project.fields['Affected versions']; instead. This however throws an exception TypeError: Cannot call method "forEach" of undefined (configure-support-issues/set-issue-fields#26)

So question is: How to access all members of enumeration, defined for the project.

3 comments
Comment actions Permalink
Official comment

Hello,

 

Please add Affected versions field to the requirements (https://www.jetbrains.com/help/youtrack/standalone/2017.2/Quick-Start-Guide-Workflows-JS.html#requirements), and then you can use ctx['Affected versions'].values .

Comment actions Permalink

Hello, Lüba thank you for the support. I have rewritten workflow as you had suggested:

//2) Setup affected version
      var versions = ctx['Affected versions'].values;     
      versions.forEach(function(v){
        if(!v.isArchived && v.isReleased )
          {
            issue.fields['Affected versions'].add(v);
          }
      });

//...

  requirements:{
    AV : {
      name : 'Affected versions',
      type : entities.ProjectVersion.fieldType,
      multi : true
    }
  }

This, however still throws TypeError: Cannot read property "values" of undefined.

However if I use defined alias AV, workflow works correctly:

var versions = ctx.AV.values;

 

Perhaps this is a bug?

 

0
Comment actions Permalink

Unfortunately this is not a bug, but as designed - object in the context is available by the name, by which it's announced in requirements.

0

Please sign in to leave a comment.