Workflow: AutoCreate SubTasks for Issue type

Hi YouTrack community!

I tried for several days to make it work, but no success, so asking for your help

The main idea -> when user creates task of "New Lead" type system automatically creates subtasks with some properties prepopulated (assignee, customer, due date, estimation). Everything is working fine for 'just copy from parent' fields, but for DueDate and Estimations not

I've read manual (+example for AutoSubtask creation), searched through the forum and googled, but stack with two points (see comments in code)

Found some examples for Due Date, but it doesn't work for me

As for Estimation, again, this example from somebody's implementation not working. More to that looks like Estimation expects number type value (i see overlay with such message, though it is strange).

Would really appreciate your help

var entities = require('@jetbrains/youtrack-scripting-api/entities');
exports.rule = entities.Issue.onChange({
  title: 'New-lead-subtasks',

  guard: function(ctx) {
    var issue = ctx.issue;
    return issue.becomesReported &&
      issue.fields.Type.name === ctx.Type.Newlead.name;
  },

  action: function(ctx) {
    var issue = ctx.issue;

    var createIssue = function(name, type) {
      var DAY_IN_MS = 24 * 60 * 60 * 1000;
      var date = new Date(Date.now() + 7 * DAY_IN_MS);

      var newIssue = new entities.Issue(ctx.currentUser, issue.project, name + ' for ' + issue.summary);

      newIssue.fields.Type = type;
      newIssue.fields.Assignee = issue.fields.Assignee;
      newIssue.fields.Customer = issue.fields.Customer;
      newIssue.fields.Billable = issue.fields.Billable;
//problem start here. Due Date not populated
//      newIssue.fields.DD = date.getTime();
//and continues here - dateTime is not definied (what I forgot?)
//     newIssue.fields.Estimation = dateTime.toPeriod('3h');
      newIssue.links['subtask of'].add(issue);
    };

    createIssue('1st version of offer', ctx.Type.Task);
  },

  requirements: {
    Type: {
      type: entities.EnumField.fieldType,
      Newlead: {
        name: 'New Lead'
      },
      Task: {}
    },
    DD: {
      type: entities.Field.dateType,
      name: 'Due Date'
    },
    Customer: {
      type: entities.EnumField.fieldType
    },
    Assignee: {
      type: entities.User.fieldType
    },
    Estimation: {
      type: entities.Field.periodType
    },
    Billable: {
      type: entities.State.fieldType
    }
  }
});
0
2 comments
Official comment

Hello,

I suggest you use the following code for setting the Due Date:

`newIssue.fields['Due Date'] = Date.now() + 7 * DAY_IN_MS;`

As for Estimation, add the date-time module for using this:

`var dateTime = require('@jetbrains/youtrack-scripting-api/date-time');`

Should you have any further questions, feel free to contact us.

Avatar
Permanently deleted user

Oleg,

thank you for quick reply and the help! it works :)

0

Please sign in to leave a comment.