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
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:-
I hope this helps to answer your question. :-)
Thank you and Kind regards,
Ram
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you @Ram Kumar Aravindakshan _Adaptavist_ !
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.