Change field value depending on State value
Answered
Hello,
I'm experimenting with the workflows. We desire to have a state called "To Review", and if someone changes the state from "To Review" to another state his / her account should be placed in a field called "Reviewer".
I have the field and state set up in a project, and have made the following workflow:
var entities = require('@jetbrains/youtrack-scripting-api/entities');
exports.rule = entities.Issue.onChange({
title: 'Set reviewer on state change',
guard: function(ctx) {
var issue = ctx.issue;
return issue.fields.isChanged(ctx.State) && issue.fields.oldValue(ctx.State) == "To Review"; // this does not
return issue.fields.isChanged(ctx.State); // this does work
},
action: function(ctx) {
var issue = ctx.issue;
issue.fields.Reviewer = ctx.currentUser;
},
requirements: {
Reviewer: {
type: entities.User.fieldType
},
State: {
type: entities.State.fieldType
}
}
});
It appears that
issue.fields.oldValue(ctx.State) == "To Review"
does not work. Can someone explain me why exactly? Thanks in advance !
Please sign in to leave a comment.
Hi,
This doesn't work because the field value, if the field is not a simple one (like string or date), is always an object, not just a string. The values have `name` property as well as some additional properties depending on field type (e.g. owned field values have also an `owner` property, etc).
So, to make the comparison work you need to update it slightly:
issue.fields.oldValue(ctx.State).name === 'To Review'
Please note that we recommend to avoid comparing a value to a string, but instead add the corresponding value to the requirements and compare with its name. This helps to avoid silly typos:
and
Thanks Mariya, it now works!
Hello Mariya,
could you please possbly help me with the similar question.
I'm trying to create my first workflow which changes the value of Degree of Completion to 100 %, if the State turns to Fixed or Verified.
This is the following rule which I attached already to my project. My expectation is that when I set "Fixed" in the State field, the value in the field Degree of Completion turns to 100%. What is wrong here? Thanks a lot in advance!!!
Oksana, please find the answer here: https://youtrack-support.jetbrains.com/hc/en-us/community/posts/360000091810-Change-field-value-depending-on-other-field?page=1#community_comment_360000079390
Thank you.