State Machine transition does not recognise the state field changing

In a state machine rule, I have the following problem:

In the action function, I want to check, if the state field of the issue is changing using the isChanged function (and the becomes function afterwards). This should obviously be true, as the transition itself is nothing but a complex way to change exactly that field (this is for debugging purposes, as my actual problem is far more complex, but I think I narrowed it down to this problem).

Curiously, this seems to be false for some reason. From a workflow perspective within the action function (and e erything this function calls), the issue looks, like it is not being touched at all - it's stage field is set to the old value and isChanged is false. See a minimal example below (the message will not print). This is actually a big problem in my setup, as I have other workflows getting called, which need to detect if the issue's stage is changing.

Is there any way to fix this or did I miss anything?

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

var states = {
'Some State': {
transitions: {
'to other': {
initial: true,
targetState: 'Other State',
action: function(ctx) {
if (ctx.issue.fields.isChanged(ctx.Stage)) {
console.log('Stage is changing');
}
}
}
}
},
'Other State': {
}
};

exports.rule = entities.Issue.stateMachine({
title: 'some-state-machine',
fieldName: 'Stage',
states: states,
requirements: {
Stage: {
type: entities.State.fieldType,
name: 'Stage'
}
}
});
2 comments
Comment actions Permalink
Official comment

Hello,

Could you please make sure this workflow runs at all? Please navigate to the Workflows page and check there are no "Requires setup" badge near this workflow (please attach a screenshot). 

Comment actions Permalink

Hi Anastasia,

yes, I am 100% sure that the workflow runs. I understood how the workflows behave now, which, in my opinion, is not very intuitive and makes some (at least my) use cases complicated :

  • The context that is passed to the guard, onEnter and action functions of a stateMachine rule, no changes are visible
  • The context that is passed to the guard and onEnter functions of an onChange rule for the same change however contains the changes as expected

So the lifecycle of the change seems to be:

stateMachine.state.transition.guard -> stateMachine.state.transition.action -> stateMachine.state.onEnter -> changes are added to context / transaction -> onChange.guard -> onChange.action

So my solution (which seems to be the only working option) is to have an onChange rule which emulates guard, action and onEnter functions for my state machine. That works but feels really wrong and generates lots of boilerplate.

0

Please sign in to leave a comment.