# Subtask Inherit Assignee

This workflow automatically sets the assignee of a subtask to the user who is assigned to work on the parent task.

| Name | @jetbrains/youtrack-workflow-subtask-inherit-assignee |
| Auto-attached | no |
| Modules |    [Assign subtask to assignee of parent issue](#inherit-assignee) (on-change)       [Update assignee for subtask when parent issue is reassigned](#update-subtasks) (on-change)     |

## Use Case

This workflow helps you manage the assignment of tasks and subtasks.

## Modules

This workflow includes two modules.

### Assign subtask to assignee of parent issue

This rule checks a subtask when it is linked to a parent task. If the subtask is not assigned, the issue is assigned to the assignee of the parent task.

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

exports.rule = entities.Issue.onChange({
  title: 'Assign subtask to assignee of parent issue',
  guard: (ctx) => {
    return !ctx.issue.links['parent for'].added.isEmpty();
  },
  action: (ctx) => {
    const issue = ctx.issue;

    const safeSetAssignee = function (subtask) {
      if (subtask.project && !subtask.project.isArchived) {
        if (subtask.project.key === issue.project.key || subtask.project.findFieldByName(ctx.Assignee.name)) {
          if (!subtask.fields.Assignee) {
            if (!issue.fields.Assignee ||
                    subtask.project.findFieldByName(ctx.Assignee.name).findValueByLogin(issue.fields.Assignee.login)) {
              subtask.fields.Assignee = issue.fields.Assignee;
            }
          }
        }
      }
    };

    issue.links['parent for'].added.forEach(safeSetAssignee);
  },
  requirements: {
    Assignee: {
      type: entities.User.fieldType
    },
    Subtask: {
      type: entities.IssueLinkPrototype,
      name: 'Subtask',
      outward: 'parent for',
      inward: 'subtask of'
    }
  }
});
```

### Update assignee for subtask when parent issue is reassigned

The next rule checks for subtasks when an issue is assigned to another user.

* If the subtasks are in an active project, the assignee for each subtask is set to the assignee of the parent task.

* If the subtasks are in an archived project, they are ignored.

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

exports.rule = entities.Issue.onChange({
  title: 'Update assignee for subtask when parent issue is reassigned',
  guard: (ctx) => {
    return ctx.issue.fields.isChanged(ctx.Assignee);
  },
  action: (ctx) => {
    const issue = ctx.issue;

    const safeSetAssignee = function (subtask) {
      if (subtask.project && subtask.isReported && !subtask.project.isArchived) {
        if (subtask.project.key === issue.project.key || subtask.project.findFieldByName(ctx.Assignee.name)) {
          if ((issue.fields.oldValue(ctx.Assignee) || {}).login === (subtask.fields.Assignee || {}).login) {
            if (!issue.fields.Assignee ||
                    subtask.project.findFieldByName(ctx.Assignee.name).findValueByLogin(issue.fields.Assignee.login)) {
              subtask.fields.Assignee = issue.fields.Assignee;
            }
          }
        }
      }
    };

    issue.links['parent for'].forEach(safeSetAssignee);
  },
  requirements: {
    Assignee: {
      type: entities.User.fieldType
    },
    Subtask: {
      type: entities.IssueLinkPrototype,
      name: 'Subtask',
      outward: 'parent for',
      inward: 'subtask of'
    }
  }
});
```

