How to check if a field is mandatory?

Ashish Grover December 6, 2011

I am building a plugin that creates subtasks of one or more given tasks. I want to copy only those fields that are mandatory from parent task to subtask. How to check which fields are required?

1 answer

1 accepted

0 votes
Answer accepted
Dieter
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.
December 7, 2011

First, you first can get fall field candidates using the Interface FieldManager. The call is

fields=fieldManager.getOrderableFields()

Then you iterate through this collection and for each Field you can call this method to decide if the field is required

public static boolean isFieldRequiredForSubIssueTypeAndProject (Long subIssueTypeId, GenericValue projectGV, Field field) throws FieldLayoutStorageException {
     	final FieldLayoutManager fieldLayoutManager = ComponentManager.getInstance().getFieldLayoutManager();      
	FieldLayout layout = fieldLayoutManager.getFieldLayout(projectGV, subIssueTypeId);
	if (layout == null || layout.getId() == null) {  
       		layout = fieldLayoutManager.getEditableDefaultFieldLayout();     
	}      
	FieldLayoutItem flo = layout.getFieldLayoutItem(field.getId());
	if (flo == null)
		return false; 
	return flo.isRequired();
}

subIssueTypeId is the id of your subtask issue type, projectGV is the GenericValue of your project. projectGV could be retrieved from the parent issue using this snippet:

issue.getProjectObject().getGenericValue()

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events