I can't share any code without specific goals... but I can point you to the correct apis
and
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)
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
it's due to global=True :)
It's working fine now for projects. How can i update issue types context?
Thanks
Ahmed
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did you solve that mystery? Trying to update 80 fields with new contexts.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.