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

Placing custom fields on specific project's screens

MonishP
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.
August 8, 2012

I am writing a plugin that:

1. creates a set of custom fields

2. lets user select projects

3. displays the generated customfields on view/edit screen of the user selected projects

I am stuck on step 3.

1. I don't know how to programatically put custom fields into specific screen

2. I don't know how to create field configurations for the customfields' visibility

Can any one please give me any pointers?

Thank you.

Monish

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
MonishP
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.
August 9, 2012

had to do a bit of digging but I figure it out. The following code adds the given custom field context for specific projects and then adds it to a screen.

Please let me know if there is a better way of doing this.
Hope this helps someone.

CustomField customfield = null;//custon filed instance here

	Long[] projectIds = GenericValueUtils.transformToLongIds(projects);
	JiraContextTreeManager treeManager = new JiraContextTreeManager(ComponentAccessor.getProjectManager(), ComponentManager.getInstance().getConstantsManager());
	FieldConfigSchemeManager fieldConfigSchemeManager = (FieldConfigSchemeManager) ComponentManager.getComponentInstanceOfType(FieldConfigSchemeManager.class);
	List<JiraContextNode> contexts = CustomFieldUtils.buildJiraIssueContexts(false, null, projectIds, treeManager);

	FieldConfigScheme configScheme = customfield.getConfigurationSchemes().get(0);

	//adds config yay!!
	if(configScheme==null){
		log.debug("Creating new config scheme");
		configScheme = fieldConfigSchemeManager.createFieldConfigScheme(null, contexts, null, customfield);
	} else {
		log.debug("Updating exisiting");
		configScheme = fieldConfigSchemeManager.updateFieldConfigScheme(configScheme, contexts, customfield);
	}
	 
	log.debug("Project: "+projects.get(0).entityName);
	FieldLayoutManager layoutManager = ComponentAccessor.getFieldLayoutManager();
	FieldScreenManager screenManager = ComponentAccessor.getFieldScreenManager(); 
	List<FieldScreen> screens = (List<FieldScreen>) screenManager.getFieldScreens();


	FieldManager fieldManager = (FieldManager) ComponentAccessor.getFieldAccessor();
	AddFieldToScreenUtil addFieldToScreenUtil =new AddFieldToScreenUtilImpl(ComponentAccessor.getJiraAuthenticationContext(), fieldManager ,screenManager);
      
      

	 addFieldToScreenUtil.setFieldScreenId(screen.getId());
	 log.debug("Screen name: "+screen.getName());

	 int index = screen.getTabs().size() -1;
	 
	 if(index<0){//does not exist so create
		FieldScreenTab fieldScreenTab = screen.addTab("ComSys Tab");
		fieldScreenTab.setPosition(0);
		log.debug("No table so making one");
	 }else { 
		//already exists so get index of the first one
		index =0;
	 }
	 log.debug("Tab name: "+screen.getTab(0).getName());
	       
	 addFieldToScreenUtil.setTabPosition(index);
	 addFieldToScreenUtil.setFieldId(new String[]{customfield.getId()});
	 addFieldToScreenUtil.setFieldPosition(String.valueOf(screen.getTab(0).getFieldScreenLayoutItems().size()));
	 ErrorCollection e = addFieldToScreenUtil.validate();
	 
	 log.debug("Validating field placement");
	 if (!e.hasAnyErrors()){
		addFieldToScreenUtil.execute();
		log.debug("Field:"+customfield.getName()+" SHOULD be now in screen");
	 } else {
		log.debug("Error placing field:\n"+e.getErrorMessages().toString());
	 }


TAGS
AUG Leaders

Atlassian Community Events