Requires Setup with onChange Workflow

I have the following workflow in the Cloud based YouTrack.  The goal is to display a message every time the custom field Stage of type state changes from 'WIP - Local' to 'STAGING'.

This is only my second workflow that I have created for YouTrack.  The first one was creating templates for various card types, it was a challenge.  If there is a better way to check and see if time spent was added, that would be great.  I have no idea how to check if time spent was added and would be happy to get the script below to work.

/**
* This is a template for an on-change rule. This rule defines what
* happens when a change is applied to an issue.
*
* For details, read the Quick Start Guide:
* https://www.jetbrains.com/help/youtrack/incloud/2019.3/Quick-Start-Guide-Workflows-JS.html
*/

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

exports.rule = entities.Issue.onChange({
title: 'Reminder to enter time spent',
guard: function(ctx) {
var issue = ctx.issue;
return issue.fields.isChanged(issue.Stage) && issue.fields.oldValue(issue.Stage) == ctx.State.from_WIP;
},
action: function(ctx) {
workflow.message('You must update the Time Spent!');
},
requirements: {
State: {
type: entities.State.fieldType,
from_WIP: {
name: 'WIP - Local'
}
}
}
});
2 comments
Comment actions Permalink
Official comment

Hello Scott,

Thank you for reaching out. You are doing great! 

Several comments from our side:
I believe you also need checking that the current Stage value is "STAGING". This can be done using becomes() method (https://www.jetbrains.com/help/youtrack/standalone/v1-Field.html#becomes). Here you can find a corresponding example: https://www.jetbrains.com/help/youtrack/standalone/Workflow-Work-Timer.html#start-timer

Besides that, you can check if a work item has been added in the current transaction using `ctx.issue.workItems.added.isNotEmpty()` method. `ctx.issue.workItems.added` is a set that contains work items added in the current transaction. Issue spent time is calculated from all its work items. 

Also, you may want to use workflow.check() instead - it will abort the operation if certain conditions are not met. For example, if a state is changed but no work items have been added. Please refer to https://www.jetbrains.com/help/youtrack/standalone/v1-workflow.html#check to get more details. 

Hope it helps. 

Comment actions Permalink

Thank you.  I will give these a try and see what happens.

0

Please sign in to leave a comment.