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.
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"]
}
})
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!
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
})
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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"]
}
})
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.