Issue with multiple choice field code

Hello,
I have an issue with coding a custom Assignee (Assign access to) field.
I have code which works fine when this custom field accepts only a single value (user), but when I modify it to accept multiple values it stops from working and does not seem to trigger. I am trying to make issue automatically visible to users selected in this field.
Here is the code in question:


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

exports.rule = entities.Issue.onChange({
title: 'Add Assignee to the "visible to" list',
guard: function(ctx) {
var fs = ctx.issue.fields;
return fs.isChanged(ctx.Assigneee);
},
action: function(ctx) {
var issue = ctx.issue;
issue.permittedUsers.add(issue.fields.Assigneee);

},
requirements: {
Assigneee: {
type: entities.User.fieldType,
name: "Assign access to"
}
}
});

Is the type: entities.User.fieldType incorrect for multiple choice field or should the code itself be modified?

Any help would be appreciated.

1
5 comments
Official comment

Hello,

Thank you for contacting us. When the rule is not executed, I suggest you open the Workflows page and make sure that this doesn't have the Required setup mark. This error may occur when requirements are set incorrectly. In your case, it is necessary to add the multi: true property because of using multiple field:

requirements: {
Assigneee: {
type: entities.User.fieldType,
name: "Assign access to",
multi: true
}
}

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

 

Maybe there is an issue with this line: issue.permittedUsers.add(issue.fields.Assigneee);?
When I added multi: true I'm unable to assign any user to the field as it keeps resetting and throws this error:

Processing issue Draft:
jetbrains.youtrack.scripts.wrappers.MutableIterableWrapper$ChangedIterable cannot be cast to jetbrains.exodus.entitystore.Entity
 
            jetbrains.youtrack.scripts.wrappers.MutableIterableWrapper.doAdd(MutableIterableWrapper.java:90)
            jetbrains.youtrack.scripts.wrappers.MutableIterableWrapper.add(MutableIterableWrapper.java:42)
            sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            java.lang.reflect.Method.invoke(Method.java:498)
            org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
            org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
            org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:32)
            org.mozilla.javascript.gen._jetbrains_youtrack_scripting_api_entities_2013._c_anonymous_30(@jetbrains/youtrack-scripting-api/entities:170)
            org.mozilla.javascript.gen._jetbrains_youtrack_scripting_api_entities_2013.call(@jetbrains/youtrack-scripting-api/entities)
            org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:32)
            org.mozilla.javascript.gen.pvkpermreqtemplateee_visibility_2010._c_anonymous_2(pvkpermreqtemplateee/visibility:12)
            org.mozilla.javascript.gen.pvkpermreqtemplateee_visibility_2010.call(pvkpermreqtemplateee/visibility)
            org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:63)
            org.mozilla.javascript.gen._execution_ctx__2022._c_anonymous_6(_execution_ctx_:88)
            org.mozilla.javascript.gen._execution_ctx__2022.call(_execution_ctx_)
            org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:405)
            org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3508)
            org.mozilla.javascript.gen._execution_ctx__2022.call(_execution_ctx_)
0

Sorry, I missed this line. This error occurs because you are trying to use Set of values as a single value. Instead, use the forEach method to iterate though values:

issue.fields.Assigneee.forEach(function(user) {
issue.permittedUsers.add(user);
});

 

 
1

Thank you very much, everything works the way I wanted to :) 

0

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

Have a great day!

0

Please sign in to leave a comment.