Add project for customfield context programmatically (via Java) [SOLVED]

G’day!

My customfield context has “applicable context” on “Apply to issues under selected projects” and there are chosen some project. I try add programmatically any project for context.

My Code (I use Jira server javadoc 8.5.6):


public FieldConfigScheme getFieldContextByName(CustomField cf, String contextName) { //VERIFIED work correct
    for (FieldConfigScheme fcs : cf.getConfigurationSchemes())
    	if (contextName.equalsIgnoreCase(fcs.getName()))
    		return fcs;
    
	return null;
}

///////

CustomField cf = /* my customfield */;
Long addProjectID = /* my project id for adding to context */;

FieldConfigScheme fieldContext = getFieldContextByName(cf, "My Field Context Name"); // My customfield context

	// list of projectId already added to context
List<Long> projIDs = fieldContext.getAssociatedProjectIds(); //VERIFIED work correct

	// add id of new project
projIDs.add(addProjectID);

	// list projects as JiraContextNode for updating context
List<JiraContextNode> addProjectContext = new ArrayList<JiraContextNode>();
ProjectManager pm = ComponentAccessor.getProjectManager();

	// 'convert' list of project id to list of JiraContextNode
for (Long pID : projIDs) {
		// in appliance with Jira JAVADOC I use constructor for become a JiraContextNode element from Projects id
	ProjectContext addProjContext = new ProjectContext(pID, pm);
	
	JiraContextNode jcn = addProjContext.getParent();
    if (jcn != null)
        addProjectContext.add(jcn);
}

	// manager for updating context scheme
FieldConfigSchemeManager fcsm = (FieldConfigSchemeManager) ComponentAccessor.getComponentOfType(FieldConfigSchemeManager.class);

	// in this method I update list of projects for context; 
	// the last arg must be CunfigurableField, but I don't find any solution for 'convert' my CustomField to ConfigurableField, but ConfigurableField implements CustomField and I hope it will be work
fcsm.updateFieldConfigScheme(fieldContext, addProjectContext, cf);

Problem:
updateFieldConfigScheme in FieldConfigSchemeManager.class - as I understand correct (according by javadoc) this method must update (binding with List) existing context for customfield, but after execute this code my context switch from “Apply to issues under selected projects” to “Global context”.

Possibly my problem is:
- wrong way to become List for method updateFieldConfigScheme
- wrong manager for update scheme
- wrong way to add project for context (but I don’t find in javadoc any other service, manager or other way)

Please, help me! =)

I found solution!
My wrong code is a part where I make a List of updating Projects. I found useful util-class CustomFieldUtils who make correct data

complete working code:

public FieldConfigScheme getFieldContextByName(CustomField cf, String contextName) { //VERIFIED work correct
    for (FieldConfigScheme fcs : cf.getConfigurationSchemes())
    	if (contextName.equalsIgnoreCase(fcs.getName()))
    		return fcs;
    
	return null;
}

///////

CustomField cf = /* my customfield */;
Long addProjectID = /* my project id for adding to context */;

FieldConfigScheme fieldContext = getFieldContextByName(cf, "My Field Context Name"); // My customfield context

	// list of projectId already added to context
List<Long> projIDs = fieldContext.getAssociatedProjectIds(); //VERIFIED work correct

	// add id of new project
projIDs.add(addProjectID);

ProjectManager pm = ComponentAccessor.getProjectManager();
	// list projects as JiraContextNode for updating context
List<JiraContextNode> addProjectContext = CustomFieldUtils.buildJiraIssueContexts(false, projIDs.toArray(new Long[projIDs.size()]), pm);

	// manager for updating context scheme
FieldConfigSchemeManager fcsm = (FieldConfigSchemeManager) ComponentAccessor.getComponentOfType(FieldConfigSchemeManager.class);

	// in this method I update list of projects for context; NOW IT WORKS!
fcsm.updateFieldConfigScheme(fieldContext, addProjectContext, cf);
1 Like