error while creating custom field using script runner

I want to create select List custom field using script runner and field will be having data from database but as I am new in this I am just creating select list custom field first. But I am facing following error . Am I following correct way to create custom field using script runner? 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.context.JiraContextNode
import com.atlassian.jira.issue.context.GlobalIssueContext
import com.atlassian.jira.issue.issuetype.IssueType;

createCF()
CustomField createCF()
{
def cfManager=ComponentAccessor.getCustomFieldManager();
def selectList=cfManager.getCustomFieldType("com.atlassian.jira.issue.customfields.impl.SelectCFType");
def selectListSearcher=cfManager.getCustomFieldSearcher("com.atlassian.jira.issue.customfields.impl.SelectCFType");

List <JiraContextNode> context= new ArrayList<JiraContextNode>();
context.add(GlobalIssueContext.getInstance());

def bugIssueType = ComponentAccessor.getConstantsManager().allIssueTypeObjects.find {it.name == "Bug"}
List<IssueType> issueTypes = new ArrayList<IssueType>()
issueTypes.add(bugIssueType)



return cfManager.createCustomField("SelectList CF","Demo purpose",
selectList,selectListSearcher, context, issueTypes )

}

 

error:

2018-01-08 14:00:46,898 ERROR [runner.ScriptFieldPreviewRunner]: ************************************************************************************* 2018-01-08 14:00:46,899 ERROR [runner.ScriptFieldPreviewRunner]: Script field preview failed for field that has not yet been created java.lang.IllegalArgumentException: Invalid module key specified: at com.atlassian.plugin.ModuleCompleteKey.<init>(ModuleCompleteKey.java:49) at com.atlassian.plugin.ModuleCompleteKey.<init>(ModuleCompleteKey.java:29) at com.atlassian.plugin.manager.ProductPluginAccessorBase.getEnabledPluginModule(ProductPluginAccessorBase.java:180) at com.atlassian.plugin.manager.ForwardingPluginAccessor.getEnabledPluginModule(ForwardingPluginAccessor.java:54) at com.atlassian.plugin.manager.EnabledModuleCachingPluginAccessor.getEnabledPluginModule(EnabledModuleCachingPluginAccessor.java:39) at com.atlassian.plugin.manager.ForwardingPluginAccessor.getEnabledPluginModule(ForwardingPluginAccessor.java:54) at com.atlassian.plugin.manager.ProductPluginAccessor.getEnabledPluginModule(ProductPluginAccessor.java:19) at com.atlassian.extension.plugins.ExtensionAccessingPluginAccessor.getEnabledPluginModule(ExtensionAccessingPluginAccessor.java:169) at com.atlassian.jira.plugin.customfield.CustomFieldTypeModuleDescriptorsImpl.getCustomFieldType(CustomFieldTypeModuleDescriptorsImpl.java:51) at com.atlassian.jira.issue.managers.CachingCustomFieldManager.getCustomFieldType(CachingCustomFieldManager.java:204) at com.atlassian.jira.issue.CustomFieldManager$getCustomFieldType.call(Unknown Source) at Script161.createCF(Script161.groovy:11) at Script161.run(Script161.groovy:7)

1 answer

Hi Vedant,

Where are you running this script? By the look of the error message it seems you're using the preview functionality of the Script Fields form, but this is not the sort of code that you would want to put in a script field, since it will be run (thus creating a custom field) every time the script field is viewed (which will probably be a lot of times). It would help us to assist you if you could describe the intended purpose of the script.

Even without knowing what the script is for, one problem I can see with it is that you are using the wrong string for the custom field type key. Instead of "com.atlassian.jira.issue.customfields.impl.SelectCFType", you should use "com.atlassian.jira.plugin.system.customfieldtypes:select". You can find information on how to construct the keys for various field types in the documentation for CustomFieldManager.

Thanks,

J

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Tuesday in Bitbucket

Upgrade Best Practices

Hello! My name is Mark Askew and I am a Premier Support Engineer for products Bitbucket Server/Data Center, Fisheye & Crucible. Today, I want to bring the discussion that Jennifer, Matt, and ...

158 views 3 6
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you