It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Create Custom Field using Groovy Script

AKASH BHARDWAJ Oct 21, 2016

Hi,

I am trying to write script (though  for learning curve), but unable to get it through. 

Could someone please help me where am I going wrong??

I am preety sure that arguments i am passing are not correct.

Any help will be greatly appreciated.

 

import com.atlassian.jira.ComponentManager; 
import com.atlassian.jira.component.ComponentAccessor; 
import com.atlassian.jira.issue.CustomFieldManager; 
import com.atlassian.jira.issue.fields.CustomField; 
import com.atlassian.jira.issue.IssueManager; 
import com.atlassian.jira.issue.Issue; 
import com.atlassian.jira.issue.MutableIssue; 
def issueManager = ComponentAccessor.getIssueManager() 
def customFieldManager = ComponentAccessor.getCustomFieldManager() 
//def cField = customFieldManager.getCustomFieldObject(12905) 
def testcustom =customFieldManager.createCustomField("testcustom", 
"testing", 
"textvalue","Bug") 
//return "Custom field name is" + cField 
return "Customfield created is " + testcustom

 

Thanks,

Akash

1 answer

1 accepted

3 votes
Answer accepted
Thanos Batagiannis [Adaptavist] Community Leader Oct 21, 2016

Hi Aksh,

Try something like the script below (is for a Text Area CF)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.context.GlobalIssueContext
import com.atlassian.jira.issue.context.JiraContextNode
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.issuetype.IssueType
createCF()
CustomField createCF() {
    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def constantsManager = ComponentAccessor.getConstantsManager()
    def taxtAreaType = customFieldManager.
        getCustomFieldType("com.atlassian.jira.plugin.system.customfieldtypes:textarea")
    def textAreaSearcher = customFieldManager.
      getCustomFieldSearcher("com.atlassian.jira.plugin.system.customfieldtypes:textsearcher")
    
	//set global context
    List<JiraContextNode> contexts = new ArrayList<JiraContextNode>()
    contexts.add(GlobalIssueContext.getInstance())

    def bugIssueType = constantsManager.allIssueTypeObjects.find {it.name == "Bug"}
    List<IssueType> issueTypes = new ArrayList<IssueType>()
    issueTypes.add(bugIssueType)
    return customFieldManager.createCustomField("Text-Area CF",
        "A newly created text area CF for testing",
        taxtAreaType,
        textAreaSearcher,
        contexts,
        issueTypes
    )
}

PS. In order to find JIRA's system custom field type and searcher keys have a look at jira/webapp/WEB-INF/classes/system-customfieldtypes-plugin.xml

AKASH BHARDWAJ Oct 23, 2016

@Thanos Batagiannis [Adaptavist] : Thanks for the reply. Though need to change 1 parameter here 

def bugIssueType = constantsManager.getAllIssueTypes().find {it.name == "Bug"}
Like Anzar Khan likes this
Vedant Kulkarni Feb 02, 2018

I tried this script by creating scripted field, field is created successfully but its scripted field not the text-area field. So can anyone help me, where I have to use this script so that it will create a desired custom field .

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in Statuspage

⭐️Would you recommend Statuspage? Leave a (honest!) review. ⭐️

Hi Community friends, We're working on sourcing more reviews on Capterra – a popular software review site –  to help teams like yours make more informed decisions when choosing an inc...

66 views 3 5
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you