youtrackyoutrack-api

Create a (subtask) link to an existing issue in YouTrack


I'm writing an Action to create default tasks for a user story. This is how far I got:

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

exports.rule = entities.Issue.action({
  title: 'Create default subtasks',
  command: 'tt-create-subtasks',
  guard: function(ctx) {
    // Condition that must be met to enable the custom command:
    return ctx.issue.fields.Type.name == "User Story";
  },
  action: function(ctx) {
    var issue = ctx.issue;
    var newIssue = ctx.issue.copy(issue.project);
    newIssue.summary = 'API spoofing';
    newIssue.fields.Type = ctx.Type.Task;
//    var link = newIssue.links.add(issue);  ????
    workflow.message('Default task created under issue ' && issue.description);
  },
  requirements: {
    Type: {
      type: entities.EnumField.fieldType,
      Task: {}
    },
  }
});

How do I create a new link that makes newIssue a subtask of issue?

I've look through what code completions offers, the documentation for the Issue property, SO questions, the workflow code that already exists in YouTrack, but I'm stuck...


Solution

  • You should specify the issue link too. The following code should work (newIssue will be a subtask of issue):

    newIssue.links['subtask of'].add(issue);
    

    You can find the example here: https://blog.jetbrains.com/youtrack/2017/12/make-it-workflow-part-4-generating-new-issues/