Get screen list for custom field with ScriptRunner

WW
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.
January 27, 2023

Background

We are performing a cleanup of our gigantic Jira Data Center instance (v9.2.0).

This particular piece is to cleanup custom fields with global contexts and assign specific projects to the contexts.

We have thousands of fields, which we're also cleaning up to get rid of. Some of the custom fields have hundreds of projects with screens associated with a custom field. Clicking in that tiny box in the UI is anxiety provoking at best.

Requirement

I want to supply the custom field and ultimately get a list of projects with screens associated with the field.

Not required

I do not want the list of fields associated with a project. I want the opposite.

I also do not want to find the projects for issues that have values for the custom fields. I already have that.

Tried

Here's what I've tried:

AssociatedScreensForCustomField

I can get the methods to work except for returning the associated screens (frustrating!)
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.screen.FieldScreenManager
import com.atlassian.jira.web.action.admin.customfields.AssociatedScreensForCustomField

Long customFieldID = 32673

CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager
CustomField customField = customFieldManager.getCustomFieldObject(customFieldID)
FieldScreenManager fieldScreenManager = ComponentAccessor.getFieldScreenManager()
AssociatedScreensForCustomField asfcf = new AssociatedScreensForCustomField(customFieldManager, fieldScreenManager)
asfcf.setCustomField(customField)
def fieldScreenTabs = asfcf.getAssociatedScreens()

return fieldScreenTabs

I get this returned

[] 

If I can get the screens (or FieldScreenTabs), I think I can work my way through the rest of the objects to get to the projects.

Ideally going directly to the associated projects would be the best thing.

PageObjects

I tried to use these, but ScriptRunner doesn't seem to like to even import anything from com.atlassian.pageobjects:

com.atlassian.jira.pageobjects.project.fields.Field
com.atlassian.jira.pageobjects.pages.admin.customfields.ViewUsedByDialog

Field contexts

The CustomField.getAssociatedProjectObjects() gets the projects in a field context. I'm cleaning up global contexts, so that won't work.

Other

I tried finding an association in the database maze, but to no avail. Google has not helped either.

Help!

Anybody have any ideas of how to get what I'm looking for?

1 answer

1 vote
scott_boisvert
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 14, 2023

Just ran into the same issue and came across your post while I was searching for an answer. Took a little bit to figure out, but I finally go this working. See the ScriptRunner script below (updated with dynamic field to select the Custom Field):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.fields.screen.FieldScreenManager
import com.atlassian.jira.issue.fields.screen.FieldScreen
import com.atlassian.jira.issue.fields.screen.FieldScreenTab
import com.atlassian.jira.issue.fields.screen.FieldScreenSchemeManager
import com.atlassian.jira.issue.fields.screen.FieldScreenScheme
import com.atlassian.jira.issue.fields.screen.issuetype.IssueTypeScreenSchemeManager
import com.atlassian.jira.issue.fields.screen.issuetype.IssueTypeScreenScheme
import com.onresolve.scriptrunner.parameters.annotation.CustomFieldPicker

@CustomFieldPicker(label = 'Custom Field', description = 'Pick a custom field', placeholder='Select custom field')
CustomField field

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
FieldScreenManager fieldScreenManager = ComponentAccessor.getFieldScreenManager()
FieldScreenSchemeManager fieldScreenSchemeManager = ComponentAccessor.getComponent(FieldScreenSchemeManager)
IssueTypeScreenSchemeManager issueTypeScreenSchemeManager = ComponentAccessor.getComponent(IssueTypeScreenSchemeManager)

Collection<FieldScreen> fieldScreens = fieldScreenManager.getFieldScreens()

Collection<Project> projects = []

fieldScreens.each { fieldScreen ->
List<FieldScreenTab> tabs = fieldScreen.getTabs()

tabs.each { tab ->
if (tab.isContainsField(field.id))
{
Collection<FieldScreenScheme> fieldScreenSchemes = fieldScreenSchemeManager.getFieldScreenSchemes(fieldScreen)

fieldScreenSchemes.each { fieldScreenScheme ->
Collection<IssueTypeScreenScheme> issueTypeScreenSchemes = issueTypeScreenSchemeManager.getIssueTypeScreenSchemes(fieldScreenScheme)

issueTypeScreenSchemes.each { issueTypeScreenScheme ->
projects = projects + (issueTypeScreenScheme.getProjects() as Collection<Project>)
}
}
}
}
}

projects.unique()
projects.each { project ->
log.warn project.name
}

 

Suggest an answer

Log in or Sign up to answer