Can't pass a variable to permittedGroups.add function.

I am trying to set the group permission for the issue based on a custom field called "visibility department". I can't do this successfully.I am not sure if my syntax wrong or my requirement statement is not complete. Can you please help me?

Basically, whatever is the value of "visibility department", I want to use it for the ctx.issue.permittedGroups.add(gn); statement.

 

Complete code:

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

exports.rule = entities.Issue.onChange({
title: 'Set "Visible to" group on submit',
guard: function(ctx) {
return ctx.issue.fields.isChanged(ctx.department);
},

action: function(ctx) {
var gn = ctx.issue.fields.department.name;
var dept = gn;
ctx.issue.permittedGroups.add(gn);

requirements: {

department: {
type: entities.EnumField.fieldType,
name: "Visibility Department",
HR_Education:{},
HR_Marketing:{}
},

HR_Marketing: {
type: entities.UserGroup
},

HR_Education: {
type: entities.UserGroup
}

}
});

0
5 comments
Official comment

Hello,

Since the permittedGroups property is represented as a Set of UserGroups, you cannot pass a group name as a parameter for the add method. Instead, pass a group that can be obtained with the findByName method:

var gn = ctx.issue.fields.department.name;
var dept = entities.UserGroup.findByName(gn);
ctx.issue.permittedGroups.add(dept);

Alternatively, you can pass the group added to requirements directly:

ctx.issue.permittedGroups.add(ctx.HR_Education);

I hope it will be helpful. Should you have any further questions, feel free to contact us.

 

Yes, it is possible to clear groups with the clear method:

ctx.issue.permittedGroups.clear();
 
1

You are always welcome! Should you have any further questions, feel free to contact us at any time. Have a great day!

1

Thank you for your response. This helps. I have a follow up question:

Is there way to clear all the previous permittedgroup entries? I couldn't find a command does that. Alternatively, I wanted to apply command for "Reset Visibility". That didn't work either. 

Is there way to do it? Thanks.

0

Perfect. Thank you for your help.

0

Please sign in to leave a comment.