Clone Field Screens Programmatically in groovy script.

FCCjira March 31, 2023

Hi All, 

Currently I'm creating groovy script that will copy project's  issue type screen scheme, keeping hierarchy of issue type screen scheme eg. 

Issue Type screen scheme
   Screen scheme 
     Field Screen -Create, EDIT, View  


I find some method to do that but its not working.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.scheme.SchemeEntity
import com.atlassian.jira.issue.fields.screen.FieldScreen
import com.atlassian.jira.issue.fields.screen.FieldScreenScheme
import com.atlassian.jira.issue.fields.screen.FieldScreenSchemeManager
import com.atlassian.jira.issue.issuetype.IssueType

def projectKey = "PROJ" // replace with the project key
def newSchemeName = "Clone of ${projectKey} Issue Type Screen Scheme"

// get the ProjectManager, IssueTypeScreenSchemeManager, and FieldScreenManager
def projectManager = ComponentAccessor.getProjectManager()
def issueTypeScreenSchemeManager = ComponentAccessor.getIssueTypeScreenSchemeManager()
def fieldScreenManager = ComponentAccessor.getFieldScreenManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// get the existing issue type screen scheme for the project
def project = projectManager.getProjectObjByKey(projectKey)
if (project == null) {
log.error("Project not found: ${projectKey}")
return
}
def scheme = issueTypeScreenSchemeManager.getIssueTypeScreenSchemeFor(project)
if (scheme == null) {
log.error("Issue Type Screen Scheme is null for project ${projectKey}")
return
}

// clone the issue type screen scheme
def newScheme = issueTypeScreenSchemeManager.copyIssueTypeScreenScheme(scheme)
newScheme.setName(newSchemeName)
issueTypeScreenSchemeManager.createIssueTypeScreenScheme(newScheme, currentUser)

// clone the screens for the new scheme
scheme.getEntities().each { SchemeEntity schemeEntity ->
if (schemeEntity.isOfType(IssueType.class)) {
def issueType = schemeEntity.getEntity()
def existingScreen = schemeEntity.getScheme()
def newScreenName = "Clone of ${existingScreen.getName()} for ${newSchemeName} (${issueType.getName()})"

def newScreen = fieldScreenManager.copyFieldScreen(existingScreen)
newScreen.setName(newScreenName)
fieldScreenManager.createFieldScreen(newScreen)

// associate the new screen with the new scheme and issue type
def newEntity = newScheme.getEntities().find { it.getEntity()?.getId() == issueType?.getId() }
if (newEntity == null) {
log.error("Issue Type ${issueType?.getName()} not found in new Issue Type Screen Scheme")
return
}
newEntity.setScheme(newScreen)
issueTypeScreenSchemeManager.updateIssueTypeScreenSchemeEntity(newEntity, currentUser)
}
}


  

2 answers

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 17, 2023

Hi @FCCjira

Were you able to get the code to work?

I am asking this because there is no straightforward approach to achieving your requirement.

However, if you can get this to work, it would be great to know how you managed this.

Thank you and Kind regards,

Ram

0 votes
FCCjira July 20, 2023

Hello thanks for your response, the only method I cant get is  get get all fields and tab from screen via groovy.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events