Script Runner - How to add a field on screen

Radhika Shinde February 1, 2023

How can I add a field on a project's field configuration and on the screens.

1 answer

0 votes
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.
February 5, 2023

Hi @Radhika Shinde 

I can't say I 100% understood your post but I think you can start with the below code.

 

import com.atlassian.jira.component.ComponentAccessor 
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.screen.FieldScreen
import com.atlassian.jira.issue.fields.screen.FieldScreenTab
import com.atlassian.jira.issue.fields.screen.FieldScreenScheme
import com.atlassian.jira.issue.fields.screen.FieldScreenSchemeManager
import com.atlassian.jira.util.ErrorCollection

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField customField = // your custom field to add to the screen

FieldScreenSchemeManager fieldScreenSchemeManager = ComponentAccessor.getComponent(FieldScreenSchemeManager.class)
FieldScreen fieldScreen = fieldScreenSchemeManager.getFieldScreen(FieldScreen.DEFAULT_SCREEN_ID)
// Get field screen tab (assuming you have only one tab or add to the default tab)
FieldScreenTab fieldScreenTab = fieldScreen.getTab(0)
// Add custom field to field screen tab
fieldScreenTab.addFieldScreenLayoutItem(customField.getId())
// Update field screen
fieldScreenSchemeManager.updateFieldScreen(fieldScreen)

List<FieldScreenScheme> fieldScreenSchemes = fieldScreenSchemeManager.getFieldScreenSchemes()
// add custom field to each scheme
for (FieldScreenScheme scheme : fieldScreenSchemes) {
scheme.addFieldScreen(fieldScreen)
ErrorCollection errorCollection = fieldScreenSchemeManager.updateFieldScreenScheme(scheme)
if (errorCollection.hasAnyErrors()) {
log.error(
"Error adding custom field to field screen scheme: " + errorCollection.getErrorMessages())
}
}


Bhakti Prasad Panda September 29, 2023

Hello @Tuncay Senturk ,

Check your code once. I don't think this is a right code.

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.
September 29, 2023

Hi @Bhakti Prasad Panda 

Thanks for your message, why do you think it is not the right code?

Bhakti Prasad Panda September 29, 2023

Hello @Tuncay Senturk ,

Please put the code in script console of scriptrunner. It will show you the errors.

We are using Jira 9.4.7 and i hope you're using the same so if you can refer below Java APIs, you won't find the methods (you've used in your code) in the article:

https://docs.atlassian.com/software/jira/docs/api/9.4.7/com/atlassian/jira/issue/fields/screen/FieldScreenSchemeManager.html

The methods are available in FieldScreenManager i suppose:

https://docs.atlassian.com/software/jira/docs/api/9.4.7/com/atlassian/jira/issue/fields/screen/FieldScreenManager.html

I would be glad too if i am wrong in here :) but just got this observation.

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.
September 29, 2023

Yes, you are right, this is quite old version I suppose. They (FieldScreenSchemeManager) need to be changed (to FieldScreenManager)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.20.11
TAGS
AUG Leaders

Atlassian Community Events