Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to update custom field context using java api?

Deleted user May 26, 2020

Hi,

I have to update custom field context using java api, any code to share?

Thanks

Ahmed

1 answer

0 votes
PD Sheehan
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.
May 26, 2020

I can't share any code without specific goals... but I can point you to the correct apis

https://docs.atlassian.com/software/jira/docs/api/8.5.1/com/atlassian/jira/issue/fields/config/manager/FieldConfigSchemeManager.html

and 

https://docs.atlassian.com/software/jira/docs/api/8.5.1/com/atlassian/jira/issue/fields/config/FieldConfigScheme.html

With these you can create/update the contest (name description, project, issuetype).

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.config.FieldConfigScheme
import com.atlassian.jira.issue.context.ProjectContext
def fieldConfigSchemeManager = ComponentAccessor.fieldConfigSchemeManager
def customFieldManager = ComponentAccessor.customFieldManager
def customField = customFieldManager.getCustomFieldObjectsByName('Scrum Team').first()
def project = ComponentAccessor.projectManager.getProjectByCurrentKey('JSP')
def configurableField = ComponentAccessor.fieldManager.getConfigurableField(customField.id)
def fieldConfigScheme = fieldConfigSchemeManager.getConfigSchemesForField(configurableField).find { config ->
config.contexts.any { context ->
context.projectObject?.key == project.key
}
}
if(!fieldConfigScheme){
//create one
def configScheme = new FieldConfigScheme.Builder().setName("some name").setDescription("some desc").toFieldConfigScheme()
def projectContext = new ProjectContext(project.id, ComponentAccessor.projectManager)
fieldConfigScheme = fieldConfigSchemeManager.createFieldConfigScheme(configScheme, [projectContext] as List, [null] as List, configurableField)
}
def context = fieldConfigScheme.contexts.find{ it.projectObject.key == project.key}

def fieldConfig = customField.getRelevantConfig(context) // << this can be used to create/delete/update options using optionsManager
//get the default value
customField.customFieldType.getDefaultValue(fieldConfig)
//set the default value
def newDefault = '' // depends on the field type, may need to be an option object is custom field is a select
customField.customFieldType.setDefaultValue(fieldConfig, newDefault)
Deleted user May 28, 2020

Hi Peter,

Thanks for your reply

The goal is to apply specific context (projects/issue types) based on screens linked to custom fields.

As per Atlassian recommendation, custom field Global context may impact performance.

Regards,

Ahmed

PD Sheehan
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.
May 28, 2020

Yeah, I have some of those conversions to do in my own environment.

I hope what I've shared will point you in the correct direction.

Deleted user May 29, 2020

Hi Peter,

Remove and update context are not working based on the below script, any idea?

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.config.manager.FieldConfigSchemeManager
import com.atlassian.jira.issue.fields.config.FieldConfigScheme
import com.atlassian.jira.issue.context.ProjectContext
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.FieldManager
import com.atlassian.jira.issue.fields.ConfigurableField
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.customfields.CustomFieldUtils
import com.atlassian.jira.issue.context.JiraContextNode
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("update custom fields context")

/*Variables to be set before executing the script*/
log.setLevel(Level.DEBUG) // Replace DEBUG per INFO to disable logging

FieldConfigSchemeManager fieldConfigSchemeManager = ComponentAccessor.fieldConfigSchemeManager
CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager
ProjectManager projectManager = ComponentAccessor.projectManager
CustomFieldUtils customFieldUtils = ComponentAccessor.getComponent(CustomFieldUtils.class)

String customFieldid = "customfield_11900"
def project = ComponentAccessor.projectManager.getProjectByCurrentKey('DX')
List<Long> projectIds = new ArrayList<Long>();
projectIds.add(project.getId());
log.debug("projectIds: $projectIds");
Long[] ids = new Long[projectIds.size()];

ConfigurableField configurableField = ComponentAccessor.fieldManager.getConfigurableField(customFieldid)

//Get the initial config
List<FieldConfigScheme> fieldConfigSchemes = fieldConfigSchemeManager.getConfigSchemesForField(configurableField)
FieldConfigScheme fieldConfigScheme = fieldConfigSchemes[0]
log.debug("Initial config : ${fieldConfigScheme.getAssociatedProjectIds()};${fieldConfigScheme.getAssociatedIssueTypeIds()}")

//update context
List<JiraContextNode> contexts = CustomFieldUtils.buildJiraIssueContexts(true, projectIds.toArray(ids),ComponentAccessor.getProjectManager());

//Remove the existing association
fieldConfigSchemeManager.removeSchemeAssociation(contexts, configurableField);
log.debug("Removing config : ${fieldConfigScheme.getAssociatedProjectIds()};${fieldConfigScheme.getAssociatedIssueTypeIds()}")

//update association
fieldConfigSchemeManager.updateFieldConfigScheme(fieldConfigSchemes[0], contexts, configurableField);
log.debug("Updating config : ${fieldConfigScheme.getAssociatedProjectIds()};${fieldConfigScheme.getAssociatedIssueTypeIds()}")

 

Thanks

Ahmed

PD Sheehan
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.
May 29, 2020

Try not using Global=True in your context builder. This means all projects.

Also, selecting fieldConfigSchemes[0] won't be reliable when you have more than 1.

Deleted user May 29, 2020

it's due to global=True :)

It's working fine now for projects. How can i update issue types context?

Thanks 

Ahmed

Tomáš Vrabec
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.
April 6, 2022

Did you solve that mystery? Trying to update 80 fields with new contexts. 

Pratibha Tambakad February 28, 2023

Hey sheehan,

I want to update the issue type in 60+ project contexts for a single custom field. Is there any way to achieve this?

 

Thanks

Pratibha Tambakad March 1, 2023

Hey Sheehan,

I am able to update Description and options but its becoming difficult to update issue type in the project context .any help would be appreciated.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events