Add spent time from workflow with time tracking enabled

I'm in a pickle with setting up a particular workflow where I want my devs to be forced to submit spent time on an issue when moving it to "Done". We have Time Tracking enabled on the project so I cannot have them directly modify the "Spent Time" field though. My code currently only prompts users when the spent time field is empty. And the other issue is that the command prompt that comes up tries to update "Time Spent" instead of using the "work" command. The internet has been 0 help with trying to figure this out and I could use some pointers. I've attached my code below. 

```

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

exports.rule = entities.Issue.onChange({
  title: 'Require Work Item Before Resolving',
  guard: (ctx) => {
    return ctx.issue.fields.becomes(ctx.State, ctx.State.Done);
  },
  action: (ctx) => {
    const issue = ctx.issue;
    var message = 'Round up to the nearest item - 1h, 2h, 4h, 1d, 2d, etc.';
    issue.fields.required("Spent Time", message);
  },
  requirements: {
    State: {
      type: entities.State.fieldType,
      Done: {}
    },
    SpentTime: {
      type: entities.Field.periodType,
      name: "Spent Time"
    }
  }
});

```

I want to note that I'm very new to the API and I'm coming from a C# background. I've lightly explored JavaScript but if I'm missing something obvious that's why!

0
4 comments

I recently discovered this post (https://youtrack-support.jetbrains.com/hc/en-us/community/posts/360000166624-Execute-a-custom-action-from-the-workflow) where there is in fact a "applyCommand" function but this doesn't quite work for my needs as I need the user to supply the amount of time they worked, the type, etc.

If there was a better way to prompt the command panel with the 'work' field automatically there, that would be the bare minimum of what I need. 

0

Hello,

Here is an example of a workflow that checks if there are some added work items when issue's state is changed to Done:

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

exports.rule = entities.Issue.onChange({
guard: function(ctx) {
return ctx.issue.fields.becomes(ctx.State, ctx.State.Done);
},
action: function(ctx) {
     workflow.check(ctx.issue.workItems.isNotEmpty(), 'You need to add spent time!');
},
  requirements: {
    State: {
      type: entities.State.fieldType,
      Done: {}
    }
}
});

However, if spent time was added previously, then it won't be triggered. So you can add an additional check for the work item author or if there were work items added in the past 5 minutes.

0

That's pretty much what I ended up going with.

Except what I wanted to do was to automatically prompt the user with the command like similar to how "required()" does but I can only do that with spent time, not work items. Because it would be great if the command line had "work" instead of "Spent Time" but instead I have a message that pops up saying "replace 'Spent Time' with 'work' and add your spent time!"

But as far as I can tell, you can't "prompt" a user or auto populate the command panel so that they only have to type in the hours spent and hit submit. 

0
Yes, you are right; unfortunately, there is no such functionality to show the work command in a command window. So now the only possible way to make logging of work time is as per my previous example or via your method.
0

Please sign in to leave a comment.