Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Display options in Single select field based on input from Component/s field using Script Runner.

Vasanta Kumaar January 20, 2023

Hi All,

I am working to Display Selective options in the Single choice field based on the values selected in Component/s Field.

I tried with the below script but not working and there were no errors in the script.

I  added the script to the Components field.

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.IssueManager

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.fields.CustomField

import com.onresolve.jira.groovy.user.FieldBehaviours

import com.atlassian.jira.issue.customfields.manager.OptionsManager

import static com.atlassian.jira.issue.IssueFieldConstants.COMPONENTS

import com.atlassian.jira.issue.*

import com.onresolve.jira.groovy.user.FieldBehaviours

import groovy.transform.BaseScript



def optionsManager = ComponentAccessor.getOptionsManager()

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def comp = getFieldById("components")

def value = comp.getValue()

def category = getFieldById("customfield_20550")

def customField = customFieldManager.getCustomFieldObject(category.getFieldId())

def config = customField.getRelevantConfig(getIssueContext())

def options = optionsManager.getOptions(config)
if (value.toString() == "01 - MAILFLOW")

{

def optionsMap = options.findAll

{

it.value in ["Domains management", "System Administration"]

}.collectEntries

{

[(it.optionId.toString()): it.value]

}

category.setFieldOptions(optionsMap)

}

else if (comp.getValue() == "02 - CLASSIFY")

{

def optionsMap = options.findAll

{

it.value in ["AWS Support", "Mail Format", "Data Model", "Bug Fixing"]

}.collectEntries

{

[(it.optionId.toString()): it.value]

}

category.setFieldOptions(optionsMap)

}

 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 20, 2023

I don't think you need to convert the list of options to a map.

But also, the components formfield value method will return an array of ProjectComponentImpl objects. So simply applying "toString()" on it will not give you predictable results.

This should be better:

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def comp = getFieldById("components")
def components = comp.value as List<ProjectComponent>
def category = getFieldById("customfield_20550")
def customField = customFieldManager.getCustomFieldObject(category.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

if (components.any { it.name == "01 - MAILFLOW" }) {
def optionsMap = options.findAll {
it.value in ["Domains management", "System Administration"]
}
category.setFieldOptions(optionsMap)
} else if (components.any { it.name == "02 - CLASSIFY" }) {
def optionsMap = options.findAll {
it.value in ["AWS Support", "Mail Format", "Data Model", "Bug Fixing"]
}
category.setFieldOptions(optionsMap)
}
Vasanta Kumaar January 22, 2023

Hi @Peter-Dave Sheehan ,

 

Thanks for the answer, I updated and checked, and it's working fine. I completely forgot the component field will return an array.

Thanks,

Vasantakumaar

TAGS
AUG Leaders

Atlassian Community Events