How to create custom filed on start of plugin

jira-dev
jira-plugin
jira-server

#1

Hi Experts ,

I am trying to create custom fields but i am getting null object of customFieldType and customFieldSearcher. Below is my code. Kindly help if I am doing something wrong.

Note: Same code is working if I upload obr file but if you do atlas-run command it is not able to give objects.

public class PluginListener implements InitializingBean, DisposableBean {
	
		 
	@Override
	public void destroy() throws Exception {
			
	}

	@Override
	public void afterPropertiesSet() throws Exception {
			
		List<IssueType> issueTypes = (List<IssueType>) ComponentAccessor.getIssueTypeSchemeManager().getDefaultIssueTypeScheme().getAssociatedIssueTypes();
		List<JiraContextNode> contexts=ComponentAccessor.getIssueTypeSchemeManager().getDefaultIssueTypeScheme().getContexts();
		CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
				
		
		CustomFieldType customFieldType =customFieldManager.getCustomFieldType("com.atlassian.jira.plugin.system.customfieldtypes:textfield");
		CustomFieldSearcher customFieldSearcher = customFieldManager.getCustomFieldSearcher("com.atlassian.jira.plugin.system.customfieldtypes:exacttextsearcher");
		
		System.out.println("-----------------------------customFieldType------------------: "+customFieldType);
		System.out.println("-----------------------------customFieldSearcher------------------: "+customFieldSearcher);
		
		
		try {
			customFieldManager.createCustomField(
					"MyCustom filed Name", "MyCustom filed Name Description", customFieldType,
					customFieldSearcher,
					contexts, issueTypes );
			
	
			
			
		} catch (GenericEntityException e1) {
			e1.printStackTrace();
		}
	}



#2

It’s possible that you are attempting to access components too early in the add-on lifecycle. Under Plugin system events, in the JIRA Plugins2 overview, there’s this:

“Due to the shortcomings of the component initialisation stage, a listener on the PluginEnabledEvent is the earliest time that you should attempt to do any serious wiring into JIRA. For example, if your plugin needs to ensure there are particular custom fields created in JIRA for the plugin to function properly, the plugin should attempt to create them during this stage, and not earlier.”

So it would be worth trying your custom field creation work in response to the PluginEnabledEvent, rather than in the afterPropertiesSet() method. If you do try that, please let us know if it works for you.


Plugin Install Event - Error using ActiveObjects in Function afterPropertiesSet (InitializingBean)
#3

Thanks @david.pinn it is working now. Below is working code:




@ExportAsService({MyPluginListener.class })
@Named("myPluginListener")
public class MyPluginListener implements InitializingBean, DisposableBean {

	private final static String PLUGIN_KEY = "your.plugin.key";
	private final EventPublisher eventPublisher;
	private final ActiveObjects activeObjects;
	
	
	
	@Inject
	public MyPluginListener(@ComponentImport EventPublisher eventPublisher, ActiveObjects activeObjects ){
		this.eventPublisher=eventPublisher;
		this.activeObjects=activeObjects;
	}
	
	

	@Override
	public void afterPropertiesSet() throws Exception {
		eventPublisher.register(this);
	}

	@Override
	public void destroy() throws Exception {
		eventPublisher.unregister(this);
	}
	
	@EventListener
	public void onPluginStarted(final PluginEnabledEvent pluginEnabledEvent)
	{
	    String startUpPluginKey = pluginEnabledEvent.getPlugin().getKey();
  	    if (PLUGIN_KEY.equals(startUpPluginKey))
	    {
  	    	generateCustomField();
	    }
	}



	private void generateCustomField() {
		List<IssueType> issueTypes = (List<IssueType>) ComponentAccessor.getIssueTypeSchemeManager().getDefaultIssueTypeScheme().getAssociatedIssueTypes();
		List<JiraContextNode> contexts=ComponentAccessor.getIssueTypeSchemeManager().getDefaultIssueTypeScheme().getContexts();
		CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
				
		
		CustomFieldType customFieldType =customFieldManager.getCustomFieldType("com.atlassian.jira.plugin.system.customfieldtypes:textfield");
		CustomFieldSearcher customFieldSearcher = customFieldManager.getCustomFieldSearcher("com.atlassian.jira.plugin.system.customfieldtypes:exacttextsearcher");
		
		System.out.println("-----------------------------customFieldType------------------: "+customFieldType);
		System.out.println("-----------------------------customFieldSearcher------------------: "+customFieldSearcher);
		
		
		try {
			customFieldManager.createCustomField(
					"MyCustom filed Name", "MyCustom filed Name Description", customFieldType,
					customFieldSearcher,
					contexts, issueTypes );
			
	
			
			
		} catch (GenericEntityException e1) {
			e1.printStackTrace();
		}
	}
}




Plugin Install Event - Error using ActiveObjects in Function afterPropertiesSet (InitializingBean)
#5

I haven’t been able to find the maven dependency for the Class PluginEnabledEvent, I want to trigger some operations when enabling/installing my add-on. Thank you for the help!


#6

Hello @oswaldo.diaz

The class com.atlassian.plugin.event.events.PluginEnabledEvent is in the lib named atlassian-plugins-api wich is a dependency of jira-api (that you may already have in your pom.xml).

    <dependency>
        <groupId>com.atlassian.jira</groupId>
        <artifactId>jira-api</artifactId>
        <version>7.10.0</version>
        <scope>provided</scope>
    </dependency>

#7

I think you can try create it in class implementsPluginUpgradeTask.
you can do something through com.atlassian.sal.api.upgrade.PluginUpgradeTask#doUpgrade