How to get custom fields based on project

Manmohit Sehgal February 13, 2017

I have a script written which can delete multiple custom fields. I want to get custom fields related to a specific project. I have looked at ProjectManager and CustomFieldManager groovy documentation but there is no method that provided that. Is there a way that we can do this? I am fairly new to groovy to any help is appreciated.

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

4 votes
Thanos Batagiannis _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.
February 13, 2017

Hi Manmohit,

Try 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.ConstantsManager

def projectId = ComponentAccessor.getProjectManager().getProjectByCurrentKey("JRA")?.id
def cfs = ComponentAccessor.getCustomFieldManager().getCustomFieldObjects(projectId, ConstantsManager.ALL_ISSUE_TYPES)

This will return to you all the custom fields configured for the project with key JRA and all the issue types

Manmohit Sehgal February 13, 2017

Hi Thanos,

Thank you for the reply but I still get all the fields not the ones related to project.

Thanos Batagiannis _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.
February 13, 2017

Hi Manmohit,

According to the API https://docs.atlassian.com/jira/7.2.0/com/atlassian/jira/issue/CustomFieldManager.html#getCustomFieldObjects-java.lang.Long-java.lang.String-

Gets a list of custom fields for a particular project and issue type,

and of course the custom fields that have a global context as well.

 

Jonny Carter
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.
February 23, 2017

Manmohit, you may just need to change the JRA in Thanos's script to match your project's key. If the projectId is coming back as null, then the getCustomFieldObjects() method will return all issue types. You could try something with some logging to see if you're getting your project or not.

import com.atlassian.jira.component.ComponentAccessor
def project = ComponentAccessor.getProjectManager().getProjectByCurrentKey("JRA")
def projectId = project?.id
log.warn("Project: $project.key : $projectId")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfs = customFieldManager.getCustomFieldObjects(projectId, [])
TAGS
AUG Leaders

Atlassian Community Events