youtrackyoutrack-api

Set custom field value in YouTrack custom workflow


I'm trying to automatically update an estimated issue duration using a numeric field called 'Estimate'.

I can access the start date and due date and take their difference in days (the intended estimate), but I am not able to update the Estimate field with this value. The error indicated is "Cannot set value to custom field Estimation", which is strangely phrased - is this not a supported functionality, or is this indicating some conversion of the value needs to take place, or something else?

/**
 * Whenever a due date is changed, if there is a start date present, autofill
 * the estimation field as the numerical difference between two dates in days
 */

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

exports.rule = entities.Issue.onChange({
  title: 'Due-date-set',
  guard: (ctx) => {
    const issue = ctx.issue;
    return issue.isChanged('Due Date');
  },
  action: (ctx) => {
    const issue = ctx.issue;
    const diff = (issue.fields.DueDate - issue.fields.StartDate)/86400/1000;
    issue.fields.Estimation = diff;
  },
  requirements: {
    DueDate: {
      type: entities.Field.dateType,
      name: 'Due Date'
    },
    StartDate: {
      type: entities.Field.dateType,
      name: 'Start Date'
    }
  }
});

Solution

  • In this case, estimation is a numeric type called a period, which measures a duration in "work" days and weeks. Using dateTime.toPeriod() will convert the difference in milliseconds to a period and the resultant period will be applied without incident.

    Note that this period will not necessarily align with what you might expect when you compute an absolute difference between two dates. The project settings associated with time tracking dictate a period is computed. This field is intended to be used for tracking duration in working hours, i.e., some amount of working hours per day and some amount of working days per week as configured in time tracking settings. Thus the absolute difference between two dates will be converted to some work duration period based on those settings, and will not in general be the same as absolute duration.