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
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>
}
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}")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you very much!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.