Properly access States in project and workItems per state

The goal is to be able to check what is the duration of the workItems per state in a workflow. In order to have a guard per specific State that does not allow to move in other states if hours are not recorded. This is the goal but we are struggling with the basics.

Basic 1: is to identify the state of the issue which if we agree can be done by ctx.issue.State

Basic2 : compare it with some specific State that could be dynamically added to the workflow. When i try to take ctx.State.Open or and place it anywhere in if condition in workflow.message i get that Open is unidentified and technically ctx.State is unidentified. So i see in code to use this becomes function and pass inside ctx.State and then the state you want to check also i see to do ===

but none of them are working what do i miss?

Basic3 : can i see work items based on state? Cause when i saw workItems there is a field issue then getting last or first workItem, the issue field was again unidentified.

workItems.last().issue was unidentified.

Basic4: where can i find the structure of the filled and how to access them from javascript?

Is this the place



Hello Kyriakos.

Feel free to refer to our YouTrack Workflow API on our Developer Portal. In particular, you can take a look at the context and requirements articles. The issue you describe in p. 1 and 2 could be due to the absence of the State field and its value in the requirements. An example with the State field and Open value in the requirement can be found here: Subtasks (this is a default workflow). 

workItems is a set property of the issue. The following should work if you'd like to extract the duration of the last work item in the issue: ctx.issue.workItems.last().duration.


Hi Dubin,

I will check the requirement part as you propose and come back for this , hope it works. For the workitems i am getting duration and it is fine i can get all that workitems and iterate to aggregate and get total duration also. The question is how to group it per state. I could have 5 h record whne task was in Open state then 20 h when it was in progress and then 2 hours On review. 

So i i want to block to go from Review to completed without recorded workItems how can i do it is the question. I tried from the workItem to get the issue as it says that it has a reference to the issue istance when the workitem was recorded but the issue is unidentified.


I'm afraid there is no indication in the work item itself which state the issue used to be in when that work item was added. To block a change from being made however, you can use the workflow.check function. A message will also be shown when the condition returns false, the condition itself can be: no work items recorded in the issue when the issue state was Review and becomes Completed. Here is an example of a workflow using this function to restrict certain behavior: Finding Multiple Issues. Note that you will need to specify the workflow module in the beginning of the code so its functions can be referenced:

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


no work items recorded in the issue when the issue state was Review and becomes Completed

means that at the same transaction of change state if user also created a workItem? How is this possible since i suppose that workItem is not created at the same time it has to exists When someone creates state he/she does not at the same time create workItem. and this phrase isn't in contradiction with the previous:

there is no indication in the work item itself which state the issue used to be in when that work item was added


If I understand your case correctly, you have the following conditions for the workflow to block the transition, the issue:

  • was in the Review state;
  • its state was being changed to Completed;
  • there were no work items recorded in the issue at that moment.

If the understanding is correct, then the suggestion stands. Let me know if I am missing something here. Providing a working example below:

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

exports.rule = entities.Issue.onChange({
  title: 'No-work-items-no-state-change',
  guard: (ctx) => {
    const issue = ctx.issue;
  return issue.was('State', 'Review') && issue.becomes('State', 'Completed') && issue.workItems.isEmpty();
action: (ctx) => {
    const issue = ctx.issue;
    const wasReview = issue.was('State', 'Review');
    const becomesCompleted = issue.becomes('State', 'Completed');
  const noWorkItems = issue.workItems.isEmpty();
    if (wasReview && becomesCompleted && noWorkItems) {
    workflow.check(false, 'Please add the spent time before marking as Completed.');
  requirements: {
    State: {
      type: entities.State.fieldType,
      'Review': {},
      'Completed': {}

I assume you did the code like this so in case that there are zero WorkItems to do the action and then from workflow.check to send message?

In this case my question is what does the next code bring back: 

  • is it going to bring all workItems of the task (in this case it might be from previous state lets say from Open or In progress state)?
  • is it going to bring workItems from current state ( how can i check it to be sure)?
  • is it going to bring workitems that happend on the state transaction(and how since these are different things? someone can not make workitem and state change at the same time it is always in sequence)

workItems is a Set object where multiple values can be stored. The isEmpty() set method checks whether the set is empty at the time of the transaction. Work items are not tied to any particular state–they are either present or not.

You can't add work items and change the state from Review to Completed in one transaction from the UI. So it doesn't really matter in this case, but if you are interested, there is an added subset which you can use to find if a value was added in the current transaction.


Probably i create a new workItem field that will hold the same states and then on create or update of the work Item i will pick the state of the issue and assign it there



Can you please clearly describe the task you are facing at the moment? This will help us suggest a relevant solution or a step in the right direction.


I think i will go with my last proposal of having a field in workItems about the state of the issue and i will set it by workflow manually. Then i will have the info that i need. Just forget my last comment i apologize for the inconvinience


Please sign in to leave a comment.