Hide Custom Field Dropdown Values for three different Jira groups
Asked by Murugesan:

Wants to hide Custom Field Dropdown Values for three different Jira groups.

JIRA Group name:  
Quality Team  
PO Leads  
Scrum Master

Drop down field name: Bug type:

Dropdown values:  
QA Bug  
UAT and PO bug  
SNOW-Prod Bug

Conditions:-

1. QA Bug value will show only for Quality team  
2. UAT Bug, PO Bug, SNOW-Prod Bug will show only for PO Leads group  
3. SNOW-Prod Bug will show only for Scrum Master group

We have already tried with below script in the Behavior.. But it's not working for the scenario. Please help us with the correct script.  

import com.onresolve.jira.groovy.user.FieldBehaviours  
import com.onresolve.jira.groovy.user.FormField  
import com.atlassian.jira.component.ComponentAccessor  
// Get Relevant field and options manager  
def customFieldManager = ComponentAccessor.getCustomFieldManager()  
def optionsManager = ComponentAccessor.getOptionsManager()  
def groupManager = ComponentAccessor.getGroupManager()  
// Assuming you have the ID of the select list field  
def selectListId = "customfield_15520" // Replace with actual field ID  
def customField = customFieldManager.getCustomFieldObject(selectListId)  
def config = customField.getRelevantConfig(getIssueContext())  
def options = optionsManager.getOptions(config)  
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()  
String groupName = \["TCX QA", "TCX PO Leads", "TCx Scrum Master"\]  
// Determine which options to display based on user's group membership  
def optionsToShow = \[\]  
if (groupManager.getUsersInGroup("TCX QA").contains(user)) {  
optionsToShow = \["QA Bug"\]  
} else if (groupManager.getUsersInGroup("TCX PO Leads").contains(user)) {  
optionsToShow = \["PO Bug", "UAT Bug", "SNOW-Prod Bug"\]  
} else if (groupManager.getUsersInGroup("TCx Scrum Master").contains(user)) {  
optionsToShow = \["SNOW-Prod Bug"\]  
}else {  
optionsToShow = \["QA Bug", "PO Bug", "UAT Bug"\]  
}  
// Filter options and set the field options accordingly  
def optionsMap = options.findAll { it.value in optionsToShow }.collectEntries {  
\[(it.optionId.toString()): it.value\]  
}  
Thank you,  
MurugesanN


Answers:
author: Murugesan
body: We go the script this scenario.  

import com.atlassian.jira.component.ComponentAccessor // Get relevant components def customFieldManager = ComponentAccessor.getCustomFieldManager() def optionsManager = ComponentAccessor.getOptionsManager() def groupManager = ComponentAccessor.getGroupManager() // Get select list field and its options def selectList = getFieldById("customfield_13235") // Replace with actual field ID def customField = customFieldManager.getCustomFieldObject(selectList.getFieldId()) def config = customField.getRelevantConfig(getIssueContext()) def options = optionsManager.getOptions(config) // Example: Determine user's group dynamically def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() def userGroups = groupManager.getGroupsForUser(currentUser).collect { it.name } // Define groupOptionsMap with actual options def groupOptionsMap = \[ "TCx Scrum Master": \["SNOW-Prod Bug"\], "TCX QA": \["QA Bug"\], "TCX PO Leads": \["UAT Bug", "PO Bug", "SNOW-Prod Bug"\] \] // Initialize selectedOptions variable def selectedOptions = null // Iterate through groupOptionsMap to find matching user group groupOptionsMap.each { groupName, validOptions -\> if (userGroups.contains(groupName)) { selectedOptions = options.findAll { it.value in validOptions }.collectEntries { \[(it.optionId.toString()): it.value.toString()\] } as Map\<String, String\> println("User belongs to ${groupName} group. Valid options: ${validOptions}") return // Exit loop once group is found } } // Set the field options based on the user's group if (selectedOptions) { selectList.setFieldOptions(selectedOptions) } else { // If user doesn't belong to any group, handle as needed (e.g., set default options) selectList.setFieldOptions(\[:\]) }


Tags: jira-software