Perfectly looking workflow always raises an exception
Hi! I hope someone can help me.
Here is my workflow:
const entities = require('@jetbrains/youtrack-scripting-api/entities');
const workflow = require('@jetbrains/youtrack-scripting-api/workflow');
exports.rule = entities.Issue.onChange({
title: 'State-change-restriction-to-noneexecutive-members',
guard: (ctx) => {
// Check if the issue state is being changed.
return ctx.issue.fields.isChanged(ctx.State);
},
action: function (ctx) {
const issue = ctx.issue;
const currentUser = ctx.user; // Assuming project leader can fetch current user information
// workflow.message('step 1');
// workflow.message( currentUser.login );
// workflow.message( issue.project.name );
// Check if the current user is the assignee and has the 'Non-executive team member' role.
if( currentUser.hasRole('Non-executive team member', issue.project) )
{
// workflow.message('step 2');
if ( issue.fields.Assignee.login != currentUser.login )
{
workflow.message('Only the assignee with the "Non-executive team member" role can change the state.');
issue.fields.State = issue.fields.oldValue(ctx.State);
}
}
},
requirements: {
// Define the custom field for issue state if it's not predefined
State: {
type: entities.State.fieldType,
name: 'State' // Replace 'State' with the actual name of your state field.
},
Assignee: {
type: entities.User.fieldType,
name: 'Assignee' // Ensure 'Assignee' matches the name of your assignee field.
}
}
});
Every time it is activated it shows this message: “Couldn't update issue field. The state-change-restriction-to-noneexecutive-members/state-change-restriction-to-noneexecutive-members rule threw an exception when processing the following issue: 2-178”
But
-The role 'Non-executive team member' exists.
-The workflow is intended to allow state change only for the user who assigned to the issue if he/she has the role ‘Non-executive team member’, otherwise it does nothing.
So I don't know what the problem is.
Please sign in to leave a comment.
Hi!
I'm Sergey from the YouTrack team.
In general, you can see a detailed stacktrace for an error in the workflow editor's console (not the browser one). It usually can point you to a faulty code line.
In your script, though, this piece is definitely invalid:
const currentUser = ctx.user
. It always evaluates to undefined. There's a predefined context propertyctx.currentUser
that you can use instead.If you have any questions, please let me know.