ScriptRunner Define specific options within a customfield based on usergroup

Michael
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.
May 22, 2023

Hi all,

I'm trying to figure out how create a behavior script to hide specific value(s) within a single select customfield based on if a user is within a specific group.

For Example:

Within the Create screen of my issue type I have #customfield_12345 (Request Options) with {Value 1, Value 2, and Value 3}

I want to remove the ability for end users to select Value 1 if they are not within the jira-administrators user group.

Can anyone help me?

Thanks,

Mike

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 22, 2023

Hi @Michael

For your requirement, you will need to use ScriptRunner's Behaviour, and you will need to add the code to the Behaviour Initialiser this time.

Below is a working sample code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

def groupManager = ComponentAccessor.groupManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager
def optionManager = ComponentAccessor.optionsManager

def
developmentGroupUsers = groupManager.getUsersInGroup('Developer')
def adminGroupUsers = groupManager.getUsersInGroup('jira-administrators')

def sampleList = getFieldByName('Sample List')
def list1CustomField = customFieldManager.getCustomFieldObject(sampleList.fieldId)
def availableConfig = list1CustomField.getRelevantConfig(issueContext)
def list1Options = optionManager.getOptions(availableConfig)

def availableOptions = []

if (developmentGroupUsers.contains(loggedInUser)) {
availableOptions = ['Behaviour', 'Listener']
} else if (adminGroupUsers.contains(loggedInUser)) {
availableOptions = ['Fragment']
}

def availableOptionsList = list1Options?.collect {
it.value in availableOptions
}

sampleList.setFieldOptions(availableOptionsList)

Please note that the working sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

When a user added to the Developer group logs in, the Sample List will display the Behaviour and Listener options. However, when a user of the jira-administrators group logs in, only the Fragment option will be accessible from the Sample List.

Below is a screenshot of the Behaviour configuration for your reference:-

behaviour_config-1.png

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

Michael
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.
May 23, 2023

Hello again @Ram Kumar Aravindakshan _Adaptavist_

If I only wanted to check for the "jira-administrator" group and then have all other groups contain the same option, would I code it like this?:

if (developmentGroupUsers.contains(loggedInUser)) {
availableOptions = ['Behaviour', 'Listener']
} else {
availableOptions = ['Fragment']
}

def availableOptionsList = list1Options?.collect {
it.value in availableOptions
}

sampleList.setFieldOptions(availableOptionsList)

Thanks,

Mike

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2023

Hi @Michael

Yes, that approach will work just fine.

Just make sure that you are using the correct group in the if/else condition.

Thank you and Kind regards,

Ram

Michael
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.
May 23, 2023

Suggest an answer

Log in or Sign up to answer