Unable to find a method to get the field configurations in jira using script runner

Anusha Raju August 24, 2022

Hi Team,

We are unable to find a method to get the field configurations in jira using script runner.
Kindly let us know if we can list all field configurations and check its corresponding field configuration scheme.
We need this to performing cleanup jobs using scriptrunner.
Please advise.

1 answer

1 vote
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.
August 26, 2022

Hi @Anusha Raju

For your requirement, you can try to invoke the FieldConfigSchemeManager object using the ComponentAccessor and filter the field configuration you are looking for.  

Something like:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.context.IssueContextImpl

def projectManager = ComponentAccessor.projectManager
def fieldConfigSchemeManager = ComponentAccessor.fieldConfigSchemeManager
def customFieldManager = ComponentAccessor.customFieldManager

def project = projectManager.getProjectByCurrentKey('MOCK')
def issueContext = new IssueContextImpl(project.id, "10004") // project id and issue type id (Bug)

def sampleTextField = customFieldManager.getCustomFieldObjectsByName('Sample Text Field').first()

log.warn "====>>> ${fieldConfigSchemeManager.getRelevantConfig(issueContext, sampleTextField).name}"

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to modify it accordingly.

In this example, I am only looking for the Schema of one field, i.e. Sample Text Field. You will need to modify the code accordingly.

I have tested this code on the ScriptRunner Console. Below is a screenshot for your reference:-

image1.png

I hope this helps to answer your question. :)

Thank you and Kind regards,

Ram 

Anusha Raju August 26, 2022

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

Thank you so much for the suggestion.

Here I don't want the information specific to any project or issuetype.

I need a list of all field configurations in our jira instance. I need the field configurations as its is displayed on the Issues >> Field Configuration Section.

I am trying to get a list and then check if it's linked to any Field Configuration Scheme.

If not I need to delete the Field Configuration.

I need to schedule this as a job to perform this cleanup monthly.

Thanks & Regards

Anusha Raju

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.
August 28, 2022

Hi @Anusha Raju

If you want it to print all the field configurations irrespective of the project or issue type, you can try something like:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.issuetype.IssueType
import com.atlassian.jira.project.Project

def projectManager = ComponentAccessor.projectManager
def fieldConfigSchemeManager = ComponentAccessor.fieldConfigSchemeManager
def customFieldManager = ComponentAccessor.customFieldManager
def projects = projectManager.projects

def output = new File('/temp/output.txt')
output.createNewFile()

def fileWriter = new FileWriter(output)

projects.each { Project project ->
project.issueTypes.each { IssueType issueType ->
customFieldManager.customFieldObjects.each { CustomField customField ->
def issueContext = new IssueContextImpl(project.id, issueType.id)
def fieldSchema = fieldConfigSchemeManager.getRelevantConfig(issueContext, customField)
if (fieldSchema) {
fileWriter.write("${fieldSchema.name}\n")
}
}
}
}

Please note that the sample code provided is not 100% exact to your environment. Hence, you will need to make the required modifications.

The ScriptRunner console can only print a maximum of 300 lines. If the total number of lines exceeds this limit, it will be truncated and not displayed, which is why I modified the code to write to a text file instead.

I hope this helps to answer your question. :)

Thank you and Kind regards,

Ram

David Harkins
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, 2024

@Ram Kumar Aravindakshan _Adaptavist_ 

Your sample is listing the configuration for all Fields,

I think what is wanted is to obtain a list of all 'Field Configurations' that would be displayed at: 

<JiraInstance>.com/secure/admin/ViewFieldLayouts.jspa

What about the Field Configurations that are not part of a Configuration Scheme assigned to a Project?

Suggest an answer

Log in or Sign up to answer