Hi Team,
I want to set different values for custom fields based on the issue type, but I can't select the same project in multiple field contexts for different issue types. I tried using a Groovy script in the scriptunner behavior's, but it didn't work. Can you assist me in achieving this?
For eg : For "Task" issue type, I need values to display A1,B2.
For "Story" issue type, I need values to display A2,B1.
The code I tried is as follows:
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.customfields.option.Options;
Issue issue = getUnderlyingIssue();
String issueType = issue.getIssueTypeObject().getName();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
OptionsManager optionsManager = ComponentAccessor.getOptionsManager();
def myField = getFieldByName("ABC");
if(issueType.equalsIgnoreCase("Task")){
CustomField customField = customFieldManager.getCustomFieldObjectByName("ABC");
FieldConfig config = customField.getRelevantConfig(issue)
Options options = optionsManager.getOptions(config);
myField.setFieldOptions(options.findAll {it.value in ['A1','B2']});
}
else
if(issueType.equalsIgnoreCase("Story")){
CustomField customField = customFieldManager.getCustomFieldObjectByName("ABC");
FieldConfig config = customField.getRelevantConfig(issue)
Options options = optionsManager.getOptions(config);
myField.setFieldOptions(options.findAll {it.value in ['A2','B1']});
}
For getting the issue type in behaviour, I think you'll be better served with the issueContext rather than the underlying issue.
issueContext is always available, but underlyingIssue will not be available during the initial issue creation.
Then, you can group your list of options before your actual logic so you don't have to repeat nearly identical code blocks.
Somethings along the following lines:
import com.atlassian.jira.component.ComponentAccessor
def optionsByType = [
'Task' : ['A1', 'B2'],
'Story' : ['A2', 'B1'],
//easily add additional mapping
]
def myField = getFieldByName("ABC");
def myFieldCf = ComponentAccessor.customFieldManager.getCustomFieldObject(myField.fieldId)
def config = myFieldCf.getRelevantConfig(issueContext)
def allOptions = ComponentAccessor.optionsManager.getOptions(config)
def options = allOptions.findAll{ it in optionsByType[issueContext.issueType.name]}
myField.setFieldOptions(options)
Make sure your context includes all the possible options and is applicable to both issue types..
That script goes into the initialiser.
Hi @PD Sheehan ,
Thank you for your quick response. I added the script on the initializer and tried with "Glide Workflow" and without "Glide Workflow" also, but the values are not displaying. Please find the attached script for your reference.
I added those 4 values to the field and applied them to both issue types in the context.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think I know where my mistake was, but let's add some logging so we can be sure.
import com.atlassian.jira.component.ComponentAccessor
def optionsByType = [
'Bug' : ['Missing Hub Confirmation', 'Missing Parcel/DocId'],
'New Feature' : ['Missing Specifications/Sample Data', 'Missing Retailer Contact'],
//easily add additional mapping
]
def myField = getFieldByName("Training Opportunity for Requester");
def myFieldCf = ComponentAccessor.customFieldManager.getCustomFieldObject(myField.fieldId)
def config = myFieldCf.getRelevantConfig(issueContext)
def allOptions = ComponentAccessor.optionsManager.getOptions(config)
log.info "Found ${allOptions.size()} options in applicable to current context in '$myFieldCf.name': ${allOptions*.value} "
def options = allOptions.findAll{ it.value in optionsByType[issueContext.issueType.name]}
log.info "${options.size()} match the values in this script for '$issueContext.issueType.name'"
myField.setFieldOptions(options)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Now, it is working as expected. Even on editing, it shows only related values. Thank you so much again @PD Sheehan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks a lot @PD Sheehan ,Above script worked for me.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.