Forums

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

Scriptrunner console: Pull / "Print" all custom fields & their IDs within a Jira Instance?

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 26, 2023

Hi all,

We are trying to get a list of all custom fields and their corresponding IDs within our instance. We realize we could go to the Custom Fields section and simply "grab" all the custom fields there, but we also need their IDs which would mean hovering over (or going into) each customfield to see the ID.

I've searched here and google for a simple script to do this to no avail. Is there a script I'm missing to do this for a whole Jira instance?

Thanks,

Mike

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Evgenii
Community Champion
June 26, 2023

Hi, @Michael 

Sure. Try this solution:

/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/

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

List<CustomField> getAllCustomFieldObjects() {
return ComponentAccessor.getCustomFieldManager().getCustomFieldObjects() as List<CustomField>
}
Evgenii
Community Champion
June 26, 2023

Added log output of names and IDs

/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/

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

List<CustomField> getAllCustomFieldObjects() {
return ComponentAccessor.getCustomFieldManager().getCustomFieldObjects() as List<CustomField>
}

getAllCustomFieldObjects().each { CustomField it ->
log.warn("${it.id} ${it.name}")
}
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 26, 2023

Hello @Evgenii

The second code worked for me. Is there a way to list these values out within the results tab? Everything is within the log tab; but when I try to pull out each log.warn into excel, everything is pulled into a single excel cell.

Thanks,

Mike

Evgenii
Community Champion
June 26, 2023

Sure. Slightly modified, to output data to result, not log. Divider - ; Plus, made sorting by field ID

/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/

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

List<CustomField> getAllCustomFieldObjects() {
return ComponentAccessor.getCustomFieldManager().getCustomFieldObjects() as List<CustomField>
}

String cfields = ""

List<CustomField> cfobjects = new ArrayList(getAllCustomFieldObjects()).sort { it.getIdAsLong() }

cfobjects.each {
cfields += "<br/>${it.id};${it.name}"
}
cfields
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 26, 2023

Thank you very much!

Like Evgenii likes this
1 vote
PD Sheehan
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 26, 2023

I have a slightly different approach that I use to make this a bit more accessible for other users in my team.

I create a custom rest endpoint in my environment that can extract all the custom fields right on their browser screen for them.

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovy.xml.MarkupBuilder

import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate
extractCustomFields(httpMethod: "GET", groups: ['jira-users']) { MultivaluedMap queryParams, String body ->
    def listOfFieldMaps = ComponentAccessor.customFieldManager.customFieldObjects.collect { cf ->
        [
                idLong : cf.idAsLong,
                idText : cf.id,
                cfname : cf.name,
                typeKey: cf.customFieldType.key
        ]
    }

    def format = queryParams.getFirst('format') ?: 'json'
    switch (format) {
        case 'json':
            return Response.ok(new JsonBuilder(listOfFieldMaps).toPrettyString()).build();
            break
        case 'html':
            def writer = new StringWriter()
            def builder = new MarkupBuilder(writer)
            builder.table {
                thead {
                    tr {
                        listOfFieldMaps[0].keySet().each { th it }
                    }
                }
                tbody {
                    listOfFieldMaps.each { row ->
                        tr {
                            listOfFieldMaps[0].keySet().each { td row[it] }
                        }
                    }
                }
            }
            return Response.ok().type(MediaType.TEXT_HTML).entity(writer.toString()).build()
            break
        case 'csv':
            def csv = new StringBuffer()
            listOfFieldMaps[0].keySet().each { csv << it + ',' }
            csv << "\n"
            listOfFieldMaps.each { row ->
                listOfFieldMaps[0].keySet().each { csv << row[it] + ',' }
                csv << "\n"
            }
            return Response.ok().type(MediaType.TEXT_PLAIN).entity(csv.toString()).build()
            break
        default:
            return Response.notAcceptable(null).entity([error: "Invalid format. Only json,csv and html currently supported "]).build()
    }
}

You can call the endpoint with format=csv, format=html, or format=json.

TAGS
AUG Leaders

Atlassian Community Events