# Subsystem Assignee

This workflow automatically assigns an issue to a specific user based on the value set in the Subsystem field.

| Name | @jetbrains/youtrack-workflow-subsystem-assignee |
| Auto-attached | yes |
| Modules |  [Set subsystem owner as assignee for new issues](#set-subsystem) (on-change rule)  |

## Use Case

This workflow lets you assign issues to specific developers who are responsible for working on a subsystem in a project. You can customize this rule to create other techniques for assigning issues automatically based on values in other fields.

## Modules

When an issue is updated, this rule checks for the following conditions:

* The Assignee field is empty.

* The issue becomes reported in this transaction or the subsystem or project is changed in a reported issue.

If either of these conditions is true and the Subsystem field is not empty, the issue is assigned to the owner of the subsystem.

### Set subsystem owner as assignee for new issues

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

exports.rule = entities.Issue.onChange({
  title: 'Set subsystem owner as assignee for unassigned issues',
  guard: (ctx) => {
    const issue = ctx.issue;
    return !issue.fields.Assignee && (issue.isChanged(ctx.Subsystem) ||
            issue.isChanged('project') || issue.becomesReported);
  },
  action: (ctx) => {
    const issue = ctx.issue;
    const fs = issue.fields;
    if (fs.Subsystem && fs.Subsystem.owner) {
      if (ctx.Assignee.values.has(fs.Subsystem.owner))
      {fs.Assignee = fs.Subsystem.owner;}
      else
      {workflow.message(
              '{0} is set as the owner of the {1} subsystem but isn\'t included in the list of assignees for issues in this project. ' +
              'The workflow that automatically assigns issues to the subsystem owner cannot apply this change.',
              fs.Subsystem.owner.fullName, fs.Subsystem.name);}
    }
  },
  requirements: {
    Assignee: {
      type: entities.User.fieldType
    },
    Subsystem: {
      type: entities.OwnedField.fieldType
    }
  }
});
```

