Forums

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

Custom filed values based on the issue type

Lakshmi CH
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.
November 28, 2023

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']});
}

 

1 answer

1 accepted

0 votes
Answer accepted
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.
November 28, 2023

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.

Lakshmi CH
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.
November 29, 2023

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.

issue types.pngfield.png

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.
November 29, 2023

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)
Like Lakshmi CH likes this
Lakshmi CH
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.
November 29, 2023

Now, it is working as expected. Even on editing, it shows only related values. Thank you so much again @PD Sheehan 

Chhaya Bandgar
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 7, 2024

Thanks a lot @PD Sheehan ,Above script worked for me.

Like Lakshmi CH likes this

Suggest an answer

Log in or Sign up to answer