Heads up! On March 5, starting at 4:30 PM Central Time, our community will be undergoing scheduled maintenance for a few hours. During this time, you will find the site temporarily inaccessible. Thanks for your patience. Read more.
×Hi all, trying to adjust visible options in the Component/s system field based on user groups.
The issue I'm running into is that compOptions returns either an empty array or null. I believe this is because the Component/s system field doesn't have options of its own, but rather pulls options for the relevant project. How do I access that project's component list as an array?
Don't mind the nitty gritty format of the code. I'll clean it up once I know it works.
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.fields.ComponentsField
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def compField = getFieldById("components")
def compOptions = compField.getFieldOptions().findAll()
//Create list of options to hide
def hideOptions = ["Software", "Other"]
//Remove options that match the names in the hideOptions list
def newOptions = compOptions.findAll() { option -> !hideOptions.contains(option) }
log.warn(compOptions)
log.warn(hideOptions)
log.warn(newOptions)
if (!Users.loggedInUser.isMemberOfGroup("Managers")){
compField.setFieldOptions(hideOptions)
}
else {
return;
}
Issue solved. To return a complete array of Components per the project, I used a component accessor and find all for a specific project (the Behavior is mapped to that project anyway).
Then, to hide options with the names that matched, I had to change to the !hideOptions.contains(options.name)
import com.atlassian.jira.project.Project
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def compField = getFieldById("components")
//Returns list of Active Components for selected project as array
def compOptions = ComponentAccessor.projectComponentManager.findAllActiveForProject(10136)
//Create list of options to hide
def hideOptions = ["Offboarding", "Internal Transfer"]
//Remove options that match the names in the hideOptions list
def newOptions = compOptions.findAll() { options -> !hideOptions.contains(options.name)}
if (!Users.loggedInUser.isMemberOfGroup("Managers")){
compField.setFieldOptions(newOptions)
}
else {
return;
}
Glad you found a solution - I started looking at this yesterday when you posted but had some other stuff to do. Came back and you resolved - thanks for sharing. I started this off being project agnostic so thought I would share that part in case anyone who comes across this needs it to be usable by more projects they could also get the project info and use that for options
// Access the project field
def projectField = getFieldById("project")
// Get the project object from the field's value
def projectId = projectField.getFormValue() as Long
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.