Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,551,879
Community Members
 
Community Events
184
Community Groups

Get all custom fields of a project using Scriptrunner

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

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.

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 Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Mar 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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events