Youtrack Workflow. Calculate how many days have passed since the card was created

188 Views Asked by At

i am trying to set a custom calculated field - "how many days since a ticket was created"

So today's date minus created date = number of days

17/12/2020 - 12/12/2020 = 5 days

I don't understand what i need to do. Some specific java script workflof?

I try a lot of googling, workflow constructor and failed

1

There are 1 best solutions below

0
sometimes_elen On

Yes, this requirement can be solved by the JavaScript workflow. I recommend you use the on-schedule rule for this purpose. You need to check the creation date and current date and set the difference to the field. The possible code could look as follows:

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

exports.rule = entities.Issue.onSchedule({
  title: 'Days-passed',
  search: '#Unresolved',
  cron: '0 0 12 * * ?',
  muteUpdateNotifications: true,
  modifyUpdatedProperties: false,
  guard: (ctx) => {
    return true;
  },
  action: (ctx) => {
    const issue = ctx.issue;
    let nowTime = Date.now();
    let createdTime = issue.created;
    let passedMS = nowTime - createdTime;
    
    //if your time tracking settings are 24/7
    issue.fields.Passed = dateTime.toPeriod(passedMS);
    
    //if your time tracking settings are custom - the more general solution
    let passedDays = Math.round(passedMS / 1000 / 60 / 60 /24);
    issue.fields.Passed = dateTime.toPeriod(passedDays + 'd');
  },
  requirements: {
    Passed: {
        type: entities.Field.periodType,
    },
  }
});

Note that this is the sample and the real conditions will differ (e.g. the search, cron and guard sections and the field name).

I hope you will find this reference helpful.