Forums

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

Get all custom fields of a project using Scriptrunner

Shreyance Shaw March 18, 2021

Need help with scripting to get all the custom fields present on the screens for a particular  project for a specific issuetype, including the transition screens.

Can anyone help ?

The associations between various schemeManagers (IssueTypeScreenScheme , or FieldScreenScheme ) is very confusing.

 

 

 

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Winnie _Adaptavist_ March 22, 2021

Hi @Shreyance Shaw,

I came up with the script below that you can run in ScriptRunner's Script Console.

What it does is that it gets all the custom fields for all issue types of the project & then compare them with all the fields (system + custom fields) on every screen (whether the screens are applied to the workflow transition or not), then return only the custom fields as the final output.

import com.atlassian.jira.component.ComponentAccessor 
import com.atlassian.jira.issue.fields.screen.*

import org.apache.log4j.Level
import org.apache.log4j.Logger
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)

//change the project key to your own value
def project = ComponentAccessor.getProjectManager().getProjectByCurrentKey("PX")
def projectId = project?.id
log.debug "Project: $project.key | $project.name | $projectId"

//get all the custom fields for the project & all issue types
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customFields = customFieldManager.getCustomFieldObjects(projectId, []).toString()
log.debug "Custom Fields: " + customFields

def fieldScreenManager = ComponentAccessor.getFieldScreenManager()
def fieldScreens = fieldScreenManager.getFieldScreens()
def output = ""
log.debug "Screens: " + fieldScreens.name

fieldScreens.each { fieldScreen ->

def tabs = fieldScreen.getTabs()
output = output + "<h1>" + fieldScreen.name + "</h1>"

tabs.each { FieldScreenTab tab ->

output = output + "<h2>" + tab.name + "</h2>" + "<h3><u>List of Custom Fields:</u></h3>"
def items = tab.getFieldScreenLayoutItems()

items.each { FieldScreenLayoutItem item ->

if (item.getOrderableField()) {

if (customFields.contains(item.getOrderableField().name)) {

output = output + "<p>" + item.getOrderableField().name + "</p>"
}
}
}
}
}

return output 

You can of course tailor this script to your requirements.

.getCustomFieldObjects() method allows you to specify the issue type of interest. Some of the transitions in your workflow may not have a transition screen. If you know the exact screen id that you're looking at, you may change the fieldScreens.each iteration (keep the code block) to:

//10000 is my screen id
def fieldScreen = fieldScreenManager.getFieldScreen(Long.parseLong("10000"))

if (fieldScreen) {

//code block

}

and you'll get the custom fields that present on a specific screen.

2021-03-23_10-21-06.png

I hope that this helps!

If this response has answered your question, please do mark it as Accepted to make it easier for other users to discover potential solutions to their questions.

ajjaiah_nagarajappa
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 29, 2022

The script still doesn't give only field screens or fields used by the project.

Because it got no reference or used the project object references.

def fieldScreenManager = ComponentAccessor.getFieldScreenManager() 
def fieldScreens = fieldScreenManager.getFieldScreens()

and it doesn't even have any methods to support that.

 

Now, What I did is below to get fields per issue type for the project(Though you get only create screen fields, I think there is similar REST API for edit screen, just add and run then combine)

 

NOTE: you need to install 'jq', 'awx' and other shell command libraries.

command to run is,

sh yourscriptname jiraProjectKey > Output.html

 

!/bin/sh

projectKey="$1"

#### Fetch issue type fields for the Jira project, report to CSV
`curl -u username:password -X GET -H 'Content-Type: application/json' "https://yourjirainstance.com/jira/rest/api/2/issue/createmeta?projectKeys=${projectKey}&expand=projects.issuetypes.fields" | jq -r '.projects[].issuetypes[] | .name + "," +.fields[].name' | sort | awk -F ',' '{if(a[$1])a[$1]=a[$1]":"$2; else a[$1]=$2;}END{for (i in a)print i, a[i];}' OFS=, | tr ':' ',' > ${projectKey}.csv`


### Covert rows to columns and HTML report
echo "<!DOCTYPE html>"
echo "<html>"
echo "<head>"
echo "<style>"
echo "table {"
echo "font-family: arial, sans-serif;"
echo "border-collapse: collapse;"
echo " width: 100%;"
echo "}"

echo "td, th {"
echo " border: 1px solid #dddddd;"
echo " text-align: left;"
echo " padding: 8px;"
echo "}"

echo "tr:nth-child(even) {"
echo " background-color: #dddddd;"
echo "}"
echo "</style>"
echo "</head>"
echo "<body>"

echo "<h2>${projectKey} Issuetype specific fields</h2>"

echo "<table>"

echo "<tr>"
while read INPUT ;
do
echo "<td>`echo $INPUT | cut -d "," -f1`</td>" ;
done < ${projectKey}.csv
echo "</tr>"
echo "<tr>"
while read INPUT ;
do
echo "<td>`echo $INPUT | cut -d "," -f2-`</td>" ;
done < ${projectKey}.csv
echo "</tr>"
echo "</table>"
echo "</body>"
echo "</html>"
0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
March 21, 2021

Hi Shreyance,

A similar question like this was asked earlier and answered.

You can find the post on this link https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-get-custom-fields-based-on-project/qaq-p/555858

I hope this helps to solve your question. :)

I am looking forward to your feedback.

 

Thank you and Kind Regards,
Ram

TAGS
AUG Leaders

Atlassian Community Events