Cover image for How to forbid the creation/linking of subtask for a card in YouTrack

How to forbid the creation/linking of subtask for a card in YouTrack

osipovsimon profile image Simon Osipov ・2 min read

If you are using JetBrains YouTrack as your Task tracker, you are probably already familiar with Workflows. This is an event-based code snippet that allows you to perform some routine tasks automatically or on schedule. These workflows are written in JavaScript, so most of the team could easily create routines they need.

Alt Text
Problem: I would like to forbid the situation when someone creates a card with type “User Story” and marks it as “subtask of” another card.

In our case, User Story is a top hierarchy card, so it can't be a subtask of any other card. But sometimes people forget that and link two cards with that type of link instead of “relates to” or “depends on”. So, how would you forbid creation or linking as subtask certain card type in Youtrack? Here is the code snippet:

var entities = require('@jetbrains/youtrack-scripting-api/entities');
var workflow = require('@jetbrains/youtrack-scripting-api/workflow');
exports.rule = entities.Issue.onChange({
  title: workflow.i18n('Task default description'),
  guard: function(ctx) {
    var issue = ctx.issue;
    return issue.links["subtask of"].added.isNotEmpty() | issue.links["subtask of"].isNotEmpty();

  action: function(ctx) {
    var issue = ctx.issue;
    var issue_type = issue.fields["Card Type"].name;
    if (issue_type === "US" | issue.becomes("Card Type", "US")) {  
      workflow.check(!issue.links["subtask of"].added.isNotEmpty() & !issue.links["subtask of"].isNotEmpty(),
"User Story can`t be subtask of any other card, only as standalone card.");
  requirements: {}

I hope that helps!
Simon Osipov
Data Engineering TG channel RUS


Editor guide