Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Export all customfields and their details

Michael
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.
June 20, 2023

Hi all,

I'm wondering if there was a way Scriptrunner could get an export for all of my customfields and their details such as:

a) Customfield Name

b) Customfield ID

c) Screen(s) used by customfield

d) Value(s)

e) Type of customfield

 

We are trying to consolidate our fields, and an export like this would save us MANY hours of time trying to retrieve it all. Any help is greatly appreciated!

Thanks,

Mike

2 answers

0 votes
Nic Brough -Adaptavist-
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.
June 21, 2023

The script given by the AI will give you the basics, but it is badly flawed.

It can't write the CSV out to anywhere that is not available to the server.   The new File("/path/to/output/file.csv") is perfectly valid, but the path in there has to refer to a local file system on the server.  If you've got a file share mounted on the server, that will work, but if not, you'll have to copy the results file down from the server somehow.

You'll also find that the {fieldType} contains utter garbage if you were to use this script.  The AI does not understand that fieldType is an object, which would be printed as an encoded string of characters.  You probably want a more human representation, to which I would add .getName() to get what you see in the UI.

For the output, you could run the script in the SR console, and copy/paste the results it gives you on-screen.  Instead of writing to a file, you could just put a "log" statement in - the console will display all of those when it runs.

As for the two lines the AI didn't even try, I have to ask if it's worth it.  A list of all custom fields is useful for evaluating a migration, but the screens it is used on, and the possible content (of select type fields) is not going to tell you much. 

I would be far more interested in how much a field has been used.  (Also, the code for getting the screen usage is horribly complex - done properly, you'll need to check screens, field context, and field configuration, and cross reference the three against each project and issue type)

Michael
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.
June 21, 2023

Hi again @Nic Brough -Adaptavist-

I don't have access to the server backend and we don't have a file share on the server either, so it sounds like I'd need to display any results I'm able to pull from a script within the SR console and then paste them into an excel file myself.

Our main goal is to have a list of details of each customfield we have within our instance.

The main reason for seeing which screens a particular customfield is on, is to see which team(s) within our instance are using a specific customfield. If pulling a different data point; such as the issue type(s) a customfield is used within would be simpiler, that would provide us with the same data we seek.

How would I parse the Ai provided code in order to display all the needed details for each customfield?

Like this?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField

def customFieldManager = ComponentAccessor.customFieldManager

// Get all custom fields
List<CustomField> customFields = customFieldManager.getCustomFieldObjects()

// Retreive each customfield
customFields.each { CustomField customField ->
def fieldName = customField.name
def fieldId = customField.id
def fieldType = customField.getCustomFieldType()?.getName() ?: ""

// Write the customfields to the console
log << "\"${fieldName}\",\"${fieldId}\",\"${fieldType}.getName()\"\n"
}

Thanks again for all the help!

~mike

Like Nic Brough -Adaptavist- likes this
Ken McClean
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.
June 21, 2023

The AI really isn't even close.  It think it's close, but it's not.

Here's a basic script that'll return most of what you're after:

 

 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def projects = ComponentAccessor.getProjectManager().getProjectObjects()

def sb = []


projects.each { project ->
    def issues = ComponentAccessor.getIssueManager().getIssueIdsForProject(project.id)
   
    issues.each { issueId ->
        def issue = ComponentAccessor.getIssueManager().getIssueObject(issueId)
       
        def customFields = customFieldManager.getCustomFieldObjects(issue)
       
        customFields.each { customField ->
            def customFieldType = customField.getCustomFieldType().getName()

            def value = issue.getCustomFieldValue(customField)
            sb.add("Custom field '$customField.name' has an id of '$customField.id' and a type of '$customFieldType'<br>")

if(value){
sb.add("'$issue' has a value of '$value' for '$customField.name' <br>")
}


        }
    }
}

return sb.toListString().replace(",", "")
Like # people like this
Nic Brough -Adaptavist-
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.
June 21, 2023

Yep, thanks @Ken McClean - that's far better.

I have to admit I was thinking in HAPI when I read the terrible AI script - this makes scripting a lot easier and shorter, and it's closer to what the AI took a guess at.  You can do stuff like "def fieldcontent = issue.getCustomFieldValue('My field')" without needing any imports or having to "get" the custom field.

@Michael - it's very complex to explore where a field is really used, so my instinct is to ask what would be most useful to you during your analysis and focus on one of the routes, not try to cross reference all of them.

What is most representative of your current setup?  You said screens already, but my instinctive go-to is "what data have we got in them?", so I want to check if screens is best for you if you're going to keep it simple.

Michael
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.
June 22, 2023

Hi again @Nic Brough -Adaptavist- and @Ken McClean

Sorry for the delayed response. @Ken McClean although that script seems to not have any issues with it, the console just hangs after I input that script. Maybe 10 minutes after the script starts, the tab does show about 301 logs - but I can't even click on the "logs" tab without the tab going black and showing an infinite "loading" circle. Is there a way to better parse the information so it doesn't make Jira "chug"?

@Nic Brough -Adaptavist-The main reason why we wanted to retreive the screens was because we include the name of each team who uses a screen within the name of that screen; hence basically grabbing the team(s) that use each customfield. So I guess I need to change my requirements to be a little less vague. (Maybe finding the issue type a customfield is associated with would be easier?)

Main Goal:
To create a list of all customfields within our Jira Instance that contains the following data for each individual customfield:

a) Customfield Name

b) Customfield ID

c) Team using customfield (Issue Type maybe?)

d) Value(s) available within the customfield

e) Type of customfield (IE: Select List [Single], Select List [Multi], etc)

0 votes
Sanjen Bariki
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.
June 20, 2023

Hi @Michael ,

 

Welcome to Atlassian Community!

Please find the below script to achieve your requirements.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField

def customFieldManager = ComponentAccessor.customFieldManager

// Get all custom fields
List<CustomField> customFields = customFieldManager.getCustomFieldObjects()

// Create a CSV file to store the custom field details
def csvFile = new File("/path/to/output/file.csv")

// Write the CSV header
csvFile << "Field Name,Field ID,Field Type\n"

// Iterate over each custom field and write its details to the CSV file
customFields.each { CustomField customField ->
def fieldName = customField.name
def fieldId = customField.id
def fieldType = customField.getCustomFieldType()?.getName() ?: ""

// Write the custom field details to the CSV file
csvFile << "\"${fieldName}\",\"${fieldId}\",\"${fieldType}\"\n"
}

csvFile.close()

 

Note :- Please change your side as your requirements link CSV path setup and all

Please Accept the answer If it helps you 😊

Regards,

Sanjen Bariki

Michael
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.
June 21, 2023

Hello @Sanjen Bariki

Would the save path for the CSV file be server side, or would that be for my local PC side? I'm trying to pull the data off the instance and house it on my local machine.

Thanks,

Mike

Michael
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.
June 21, 2023

If the CSV can only be saved server side, is there a way to display all my customfields and their details within the console so I can copy the needed data from the console to an Excel file myself on my end locally?

Thanks again,

Mike

Nic Brough -Adaptavist-
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.
June 21, 2023

I'm afraid this answer was given by an AI that does not understand the question or Atlassian software, and it's not going to be able to answer your follow-up questions.

Michael
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.
June 21, 2023

Hi @Nic Brough -Adaptavist-

Good to know, thank you. Is there a way for us to use scriptrunner in order to pull an export of the above?

Thanks again,

Mike

Ken McClean
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.
June 21, 2023

I'll put something together for you, Michael.  Give me a few minutes.

Suggest an answer

Log in or Sign up to answer