Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to restrict a project picker selection list?

Karen Hayden May 17, 2018

We have a custom field using the Project Picker field type.  I would like to restrict the options in the drop down to Projects with a specific Project Category assignment.  (By default all projects in Jira are in the selection list.)

We have ScriptRunner for Jira, but I'm no developer and have no clue how to write a script for this and I haven't found any close examples I can edit.

 

5 answers

6 votes
Ramakrishnan Srinivasan
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.
February 19, 2019 edited

I added one more condition to make sure there is indeed  a project category associated to the project object, the below code worked for me; lookout for if(it.projectCategoryObject) condition in last section

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.ProjectManager

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def projectManager = ComponentAccessor.getProjectManager()
def projects = projectManager.getProjectObjects()

// customfield_14503 is my Single Project Select List Custom Field
def formField = getFieldById("customfield_14503")
def customField = customFieldManager.getCustomFieldObject("customfield_14503")

def prList = ComponentAccessor.getProjectManager().getProjectObjects()

formField.setFieldOptions(projects.findAll{
if(it.projectCategoryObject){
it.projectCategoryObject.name in ["Engineering", "Common"]
}
})

 

André
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 20, 2019

Hi Ramakrishnan, the code you provided works spot on. I placed it in the Initialiser of a Behaviour setting and go immediate result. Many thanks!

Kisgergely Zsolt August 2, 2019

Thank you. Perfect solution

Patrik Grönvall October 29, 2020

Thanks a lot, just the solution I waas looking for! :-)

1 vote
Tuncay Senturk
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 19, 2018

If you use Behaviors in Script Runner, you can restrict some dropdown options. The code would be similar as below, I just prototyped, so please check the code 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.ProjectManager


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def projectManager = ComponentAccessor.getProjectManager()
def projects = projectManager.getProjectObjects()

def formField = getFieldById("customfield_11100")
def customField = customFieldManager.getCustomFieldObject("customfield_11100")
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
long projectCategoryId = issue.projectId

formField.setFieldOptions(options.findAll {
projectManager.getProjectObj(it.value as long).projectCategoryObject.id == projectCategoryId
})
Mani Reddy June 21, 2018

@Tuncay Senturk

The above code produces the following error, when added it to the server side scripts in Behaviors.

"The variable [issue] is undeclared"

So I modified it to  "underlyingIssue" and the errors gone. However I am still unable to restrict options from the projects pickers filed.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.ProjectManager


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def projectManager = ComponentAccessor.getProjectManager()
def projects = projectManager.getProjectObjects()

def formField = getFieldById("customfield_13414")
def customField = customFieldManager.getCustomFieldObject("customfield_13414")
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
long projectCategoryId = underlyingIssue.projectId

formField.setFieldOptions(options.findAll {
projectManager.getProjectObj(it.value as long).projectCategoryObject.id == projectCategoryId
})

Any help would be appreciated.

Mani Reddy June 21, 2018 edited

Adding some more details to the issue, after adding the above script and mapping it to the projects, the field does not populate any options on the edit screen.

Same field on the Create screen is displaying all the options again. I kind a confused.

Empty Project Teams.PNG

0 votes
Richard Duffy
Contributor
August 8, 2023 edited

Hi

I have being trying to adjust the above script to not use Category but use the project "Key"

Cant seem to get it working, can anyone help?

Thanks

Richard

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.project.ProjectManager

 

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def projectManager = ComponentAccessor.getProjectManager()

def projects = projectManager.getProjectObjects()

 

// customfield_15206 is my Single Project Select List Custom Field

def formField = getFieldById("customfield_15206")


 

def prList = ComponentAccessor.getProjectManager().getProjectObjects()

 

formField.setFieldOptions(projects.findAll{

     if(it.projectCategoryObject){

        it.projectCategoryObject.name in ["Squad"]

     }

})
0 votes
Alejandro Reyes October 1, 2018

Hi, i tried myself and found that the options are not getting retrieved, so i used the projects variable to get all the projects, search for which ones are for the categories i want and then set the values:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.ProjectManager

def projectManager = ComponentAccessor.getProjectManager()
def projects = projectManager.getProjectObjects()

def formField = getFieldById("customfield_10124")
def projectCategoryId = underlyingIssue.getProjectObject().getProjectCategory().id

formField.setFieldOptions(projects.findAll{it.projectCategoryObject.name in ["Iniciativas", "Área Vela"]})

Hope it helps.

0 votes
laralg
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 18, 2018

Hi Karen,

By default I think the project picker only lets you pick the project you can see. So if you limited the view of the other projects, you will limit the projects they can pick.

The downside of this, is that the people will lose too the access to the projects so maybe this is not what you are looking for.

If you have scriptrunner maybe you can do that with behaviours but I am not sure if you will need to code a little bit.

 

Regards

Suggest an answer

Log in or Sign up to answer