How to remove an option from a field using behavior plugin

Akhil_vs August 30, 2017

Hello All,

 

I am trying to remove 2 options from a field for a particular group using behaviour plugin. I need the code to remove 2 values from the custom field.

EX: say the custom field "Type" has option a,b,c,d

I want the option a and b to be visible to user group and option a.b.c and d to be visible to developer and admin group.

 

Thanks,

Akhil

3 answers

1 vote
Akhil_vs September 18, 2017

Hello Stephan,

 

I need your help with this:

if ( GroupManager.isUserInGroup(currentUser,"abc_group"))
{
def optionsMap = options.findAll {
it.value in ["Feature Request","Bug"] // list of options you want to show
}.collectEntries {
[
(it.optionId.toString()) : it.value
]
}


formField.setFieldOptions(optionsMap)

 

My code seems to be correct and there is no error , but still all the 4 options are displaying instead of the 2 mentioned in code.

Akhil_vs September 18, 2017

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.crowd.embedded.api.Group
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.security.groups.GroupManager
import java.util.List

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def groupManager = ComponentManager.getComponentInstanceOfType(GroupManager.class)
def formField = getFieldByName("Type") // *name* of your custom field
def customField = customFieldManager.getCustomFieldObject(formField.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def userUtil = ComponentAccessor.getUserUtil()
def currentUser = ComponentAccessor.JiraAuthenticationContext.getLoggedInUser()

Stephen Cheesley _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 20, 2017

In what context have you used this script (i.e. Script Field, Behavior, Web Fragment, e.t.c.)?

Akhil_vs September 20, 2017

I have used this script in behavior and applied as a validator script to field "Type" mentioned in script . I don't find any error in this script and it should work. But unfortunately it's not

0 votes
Stephen Cheesley _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 2, 2017

Hi Akhil,

Apologies for the late reply.

I've taken a further look and it is possible to do what you need with behaviours. There is an example in the ScriptRunner Documentation. This example shows the basic code that you need.

I have taken the liberty of writing you a script which should do the job you want.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

// ============================================
// You only need this bit if you are running from your script root
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours
// ===========================================

// Only perform the validation if we are on the "Create Screen"
if (!getActionName() in ["Create Issue", "Create"]) {
return // not the initial action, so don't set default values
}

// ::.. Parameters ..::
// You can calibrate the following options to get the options field setup as you prefer.
def fieldName = "A Test List" // <- TODO: Set field name
def availableOptionsPerGroup = [
"Administrators" : ["c", "d"],
"Users" : ["a", "b"]
] // <- TODO: Set your preferred options

// ::.. Managers ..::
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

// ::.. Retrieve Field Options ..::
def testList = getFieldByName(fieldName)
def customField = customFieldManager.getCustomFieldObject(testList.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

// ::.. Get group of current user ..::
def currentUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def remoteUsersRoles = projectRoleManager.getProjectRoles(currentUser, issueContext.projectObject)*.name

// ::.. Get all of the values that are applicable for the groups the user is in ..::
def values = availableOptionsPerGroup.subMap(remoteUsersRoles).values().flatten()

// ::.. Display the available options ..::
def validOptions = options.findAll { it.value in values }
testList.setFieldOptions(validOptions)

This script will setup the options in the availableOptionsPerGroup map depending on the groups and the options you want to display (I believe I followed a use case that matches your original request).


Remove not Set
The script that I have provided will set these options. We do a find, so it will only allow options to be set that were configured against the field (i.e. as-is won't allow options that aren't available). You could modify the script to remove options if you prefer.


Setup
You must use this script in an initialiser and NOT a field condition. You will need to set the field name and available options that you want before the script will work as expected. I have tested locally and it seems to work as expected with no errors.


I hope this helps!


Steve

0 votes
Stephen Cheesley _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 6, 2017

Hi Akhil,

I've been having a look to see if this is possible. It is possible using behaviours to set default options for a field, but there is not a way to modify the options on show to a user using behaviors.

There is, however a workaround! The way that I made this work was:

  1. Create two checkbox custom fields with the same name.
  2. Give one of the fields the options for user and one of them the options for admin
  3. Set up a new behaviour and add the both of those fields.
  4. Set the behaviour of the user list to "Hide" and set the condition as "When current user in group: jira-administrators"
  5. Set the behavior of the admin list to "Hide" and set the condition as "When current user in group: jira-users except when current user in group: jira-administrators"
  6. Ensure that you set up your mappings correctly

This will the show the "Users" version of the list for normal users and the "Admins" version of the list for jira-administrators. It means having two versions of the list with the same name, but if you specify which is which in the description they will be easy to identify.

Hope this helps!

Akhil_vs September 10, 2017

Hello Stephen,

Thanks for your quick reply.

Creating 2 custom fields is not suggested in our environment . I would like to implement this in a script.

I have written a script for this but I am facing an issue. I am able to see the all the options when I am in both the user and admin group. 

User should only be able to see 2 options.

Suggest an answer

Log in or Sign up to answer