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

Behavior script for field dependecy

Jiri Kanicky February 18, 2020

Hi.

I have two drop down fields:

  • Environment (Custom Field 13821)
    • Dev
    • UAT
    • Prod
  • Severity (Custom Field 10120)
    • Sev 1
    • Sev 2
    • Sev 3

 

I would like to create behavior as follows:

If user selects Dev or UAT, Sev 1 is hidden is the drop down. In other words Sev 1 is only available for Prod.

I have managed to hide the whole field Severity, but not sure how to hide just one option from that field

def environmentField = getFieldById(getFieldChanged())

def selectedOption = environmentField.getValue() as String

def cfSeverity = getFieldById("customfield_10120")

if (selectedOption == "Production") {
cfSeverity.setHidden(true)
}
if (selectedOption != "Production") {
cfSeverity.setHidden(false)
}

Can you help?

Thank you.

 

 

 

 

2 answers

Suggest an answer

Log in or Sign up to answer
1 vote
Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 19, 2020

Hi @Jiri Kanicky,

Below code will help you to hide/show only particular options of select list field, I didn't test this out in my app, it may require minor changes

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor

//Get Relevant field and options manager
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def environmentField = getFieldById(getFieldChanged())
def selectedOption = environmentField.getValue() as String
def cfSeverity = getFieldById("customfield_10120")

def customField = customFieldManager.getCustomFieldObject(cfSeverity.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

if(selectedOption == "Production"){
def optionsMap = options.findAll {
it.value in ["Sev 1", "Sev 2", "Sev 3"] // list of options you want to show
}.collectEntries {[(it.optionId.toString()): it.value]}
cfSeverity.setFieldOptions(optionsMap)
}
else{
def optionsMap = options.findAll {
it.value in ["Sev 2", "Sev 3"] // list of options you want to show
}.collectEntries {[(it.optionId.toString()): it.value]}
cfSeverity.setFieldOptions(optionsMap)
}

 

BR,

Leo

Jiri Kanicky February 19, 2020

Hi @Leo

Your code works like a charm. I did not have to change anything.

I was able to manage it to the following script myself, but I was not able to figure out where the problem is. Your example really helped me to find the problematic part. I guess I was very close :).

My NOT working script:

//working but for last line
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours

def environmentField = getFieldById(getFieldChanged())
def selectedOption = environmentField.getValue() as String
def cfSeverity = getFieldById("customfield_10120")

def severityField = customFieldManager.getCustomFieldObject(getFieldChanged())
def config = severityField.getRelevantConfig(getIssueContext())
def options = ComponentAccessor.optionsManager.getOptions(config)

def allowedOptions = null

if (selectedOption == "Production") {
allowedOptions = ComponentAccessor.optionsManager.getOptions(config).findAll {
it.value in ["High",]
}
}
severityField.setFieldOptions(allowedOptions)
Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 19, 2020

Couple of corrections in your code

1. We need to get the config of field on which options needs to be hidden. here Severity field

so below line 

def severityField = customFieldManager.getCustomFieldObject(getFieldChanged())

 should be changed to 

def severityField = customFieldManager.getCustomFieldObject(cfSeverity)

2.  "," should be removed from below line after "High" value

it.value in ["High",]

 

Hope this helps :)

Jiri Kanicky February 19, 2020

Yep, this was the missing part I was not able to figure out for several hours :). Thanks Leo!

Vidhya Mohan February 25, 2020

Hello @Leo 

Can you please let me know why this is not working for me? Also I want to set None as the default value.

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

//Get Relevant field and options manager
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def detectedinteststageField = getFieldById(getFieldChanged())
def selectedOption = detectedinteststageField.getValue() as String
def cfDevRCA = getFieldById("customfield_18408")

def customField = customFieldManager.getCustomFieldObject(cfDevRCA.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

if(selectedOption != "5-Warranty" || selectedOption != "7-PROD"){
def optionsMap = options.findAll {
it.value in ["None", "Ambiguous requirement" , "Code merging" , "Data quality issue" , "Design documentation issue - E2E sheet" , "Design gap - FDD" , "Design gap - TDD" , "Developer Oversight" , "Existing prod issue" , "Impact not analysed - Cross project impact" , "Impact not analysed - Incomplete analysis" , "Impact not analysed - Regression/Defect fix" , "Improper reconciliation" , "Incomplete implementation/defect fix in ST/UAT/Prod" , "Incorrect configuration" , "Incomplete implementation" , "Incorrect logic" , "Incorrect solution" , "Induced due to PRS" , "Interface changes/mismatch" , "Lack of system understanding" , "Package issue" , "Retrofit missed" , "Scenario coverage/Incomplete unit testing"
] // list of options you want to show
}.collectEntries {[(it.optionId.toString()): it.value]}
cfDevRCA.setFieldOptions(optionsMap)
}
else{
def optionsMap = options.findAll {
it.value in ["None", "Ambiguous requirement" , "Code merging" , "Data quality issue" , "Design documentation issue - E2E sheet" , "Design gap - FDD" , "Design gap - TDD" , "Developer Oversight" , "Impact not analysed - Cross project impact" , "Impact not analysed - Incomplete analysis" , "Impact not analysed - Regression/Defect fix" , "Improper reconciliation" , "Incomplete implementation/defect fix in ST/UAT/Prod" , "Incorrect configuration" , "Incomplete implementation" , "Incorrect logic" , "Incorrect solution" , "Induced due to PRS" , "Interface changes/mismatch" , "Lack of system understanding" , "Package issue" , "Retrofit missed" , "Scenario coverage/Incomplete unit testing"
] // list of options you want to show
}.collectEntries {[(it.optionId.toString()): it.value]}
cfDevRCA.setFieldOptions(optionsMap)
}
Jiri Kanicky February 28, 2020

Hi @Leo 

Please can you advise why the same does not work for Components field.

I would need to do the same for Components as for Severity, but just changing the field to Components does not seem to work.

I also tried to follow this article , but that does not seem to work either.

Is there any different mechanism how to restrict values for Components field?

Thank you for your help.

0 votes
Jiri Kanicky February 19, 2020

I manage to create script which hides the whole Severity field. Please can you advise how I can hide just one option from the Severity field?

def environmentField = getFieldById(getFieldChanged())

def selectedOption = environmentField.getValue() as String

def cfSeverity = getFieldById("customfield_10120")

if (selectedOption == "Production") {
cfSeverity.setHidden(true)
}
if (selectedOption != "Production") {
cfSeverity.setHidden(false)
}
TAGS
AUG Leaders

Atlassian Community Events