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 !

0
4 comments
Avatar
Permanently deleted user
Official 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:

    issue.fields.oldValue(ctx.State).name === ctx.State.ToReview

and

    State: {
      type: entities.State.fieldType,
ToReview: {
name: 'To Review'
}

    }
Avatar
Permanently deleted user

Thanks Mariya, it now works!

0

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!!!

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

exports.rule = entities.Issue.onChange({
  title: 'Set degree of completion to 100% when Fixed or Verified',
  guard: function(ctx) {
  return ctx.issue.becomesResolved;
  },
  action: function(ctx) {
  ctx.issue.fields.DegreeOfCompletion = '100%';
  },
  requirements: {
    DegreeOfCompletion: {
      name: 'Degree of completion',
      type: entities.Enum.fieldType
    }
  }
});
0

Please sign in to leave a comment.