# Due Date

This workflow helps you set deadlines for resolving issues.

| Name | @jetbrains/youtrack-workflow-due-date |
| Auto-attached | no |
| Modules |    [Require due dates for submitted issues](#due-date-required) (on-change rule)       [Notify assignee about overdue issues](#notify-assignee) (on-schedule rule)     |

## Use Case

This workflow requires that every reporter sets a due date for resolving an issue and notifies the assignee when an issue is overdue.

To use this workflow, you should either add a Due Date field to your project or create another date-type field and modify the reference to the Due Date field in the default workflow.

This workflow was originally taken from a submitted request ([JT-5998](https://youtrack.jetbrains.com/issue/JT-5998)).

The user who submitted this issue wanted to make a field required for all new issues so that it could be used as the basis for a report.

## Rules

The first module contains a rule that prevents the creation of an issue unless the due date is set. If the Due Date field is empty, a warning is displayed.

You can easily modify this rule to require values for one or more custom fields in your project. However, for the rule to work properly, the required fields should have the Can be empty setting  enabled. For more information, see [https://www.jetbrains.com.cn/en-us/help/youtrack/cloud/?Attaching-Custom-Fields-to-Projects](https://www.jetbrains.com.cn/en-us/help/youtrack/cloud/?Attaching-Custom-Fields-to-Projects).

### Require due dates for submitted issues

First, the rule verifies that the issue has not been reported. This means the warning is displayed when the user is still creating the issue or working with an issue draft. If true, it checks for a value in the Due Date field with the `required` method.

* If the Due Date field is empty, a warning is displayed. The required field is highlighted. The user cannot create the issue.

* If the Due Date is set, the user can create the issue.

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

exports.rule = entities.Issue.onChange({
    title: 'Require due dates for submitted issues',
    guard: (ctx) => {
        return ctx.issue.becomesReported;
    },
    action: (ctx) => {
        ctx.issue.fields.required(ctx.DueDate, 'You must set the Due date!');
    },
    requirements: {
        DueDate: {
            type: entities.Field.dateType,
            name: 'Due Date'
        }
    }
});
```

> **Warning: Localized Messages**
> The original workflow script provided by YouTrack uses the `workflow.i18n` function. This is an internal function that is only meant to be used to reference localized message strings. To ensure that you can copy and customize this workflow code, we have removed this function from the sample. If you are editing the default workflow and want to customize any message text, we recommend that you remove these functions from your code as well.
>
>
>
> To learn more about localized messages in workflows, see [Localized Workflow Messages](Workflow-Localization.html).

### Notify assignee about overdue issues

The second module contains a rule that checks the value in the Due Date field at 10:00. If the issue is overdue and unassigned, notification is sent to the project owner (referenced here as `issue.project.leader`). If the issue is assigned, notification is sent to the assignee.

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

exports.rule = entities.Issue.onSchedule({
  title: 'Notify assignee about overdue issues',
  search: '#Unresolved has: {Due Date}',
  cron: '0 0 10 ? * MON-FRI',
  guard: (ctx) => {
    return ctx.issue.fields.DueDate < Date.now();
  },
  action: (ctx) => {
    const issue = ctx.issue;
    let userToNotify = issue.fields.Assignee;
    if (!userToNotify) {
      userToNotify = issue.project.leader;
    }
    const formattedDate = dateTime.format(issue.fields.DueDate);
    const notificationText = 'Issue became overdue on <i>{0}</i>:', formattedDate +
            ' <a href="' + issue.url + '">' + issue.summary + '</a><p style="color: gray;font-size: 12px;margin-top: 1em;border-top: 1px solid #D4D5D6">' +
            'Sincerely yours, YouTrack' + '</p>';
    userToNotify.notify('[YouTrack, Issue is overdue]', notificationText);
  },
  requirements: {
    DueDate: {
      type: entities.Field.dateType,
      name: 'Due Date'
    },
    Assignee: {
      type: entities.User.fieldType
    }
  }
});
```

> **Warning: Localized Messages**
> The original workflow script provided by YouTrack uses the `workflow.i18n` function. This is an internal function that is only meant to be used to reference localized message strings. To ensure that you can copy and customize this workflow code, we have removed this function from the sample. If you are editing the default workflow and want to customize any message text, we recommend that you remove these functions from your code as well.
>
>
>
> To learn more about localized messages in workflows, see [Localized Workflow Messages](Workflow-Localization.html).

