It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptRunner - Hide Checkboxes with Behaviours Edited

Using ScriptRunner Behaviours on Jira Server. I have a drop down field that I'm using where I want the checkbox field to show or hide dependent on the selection as well as update the options for the checkbox and make the checkboxes required or not required. The checkbox options is working, making the checkboxes required works. The hide and show is not working. If I remove the code to set the options, the hide/show works fine. Looking for a little direction here. Thanks in Advance. Note it should hide the policySymbols field when area field is N/A and show it for all other selections.

Areas is the drop down and policySymbols is the checkbox field. I have the script on the areas field.

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def policySymbols = getFieldByName("Policy Symbols")
def area = getFieldByName("Area")

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

def areaVal = area.getValue()

if (areaVal == "All") {
def optionsMap = options.findAll {
it.value in ["CL-BP", "CL-CA", "CL-CPP", "CL-CT", "CL-CUP", "CL-DO", "CL-EO", "CL-EPL", "CL-F", "CL-FO", "CL-FUP", "CL-GLA", "CL-IMP", "CL-OE", "CL-SCP", "CL-WCP", "CL-XA", "CL-XMP", "CL-XPP", "PL-AHP", "PL-AR", "PL-DF", "PL-FA", "PL-FP", "PL-GH", "PL-HM", "PL-HO", "PL-IM", "PL-MA", "PL-MC", "PL-MH", "PL-NSA", "PL-PA", "PL-PA3", "PL-PLH", "PL-PPT", "PL-PRH", "PL-SH", "PL-TH"]
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}

policySymbols.setFieldOptions(optionsMap)
policySymbols.setRequired(true)
policySymbols.setHidden(false)
}
else if (areaVal == "Commercial Lines") {
def optionsMap = options.findAll {
it.value in ["CL-BP", "CL-CA", "CL-CPP", "CL-CT", "CL-CUP", "CL-DO", "CL-EO", "CL-EPL", "CL-F", "CL-FO", "CL-FUP", "CL-GLA", "CL-IMP", "CL-OE", "CL-SCP", "CL-WCP", "CL-XA", "CL-XMP", "CL-XPP"]
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}

policySymbols.setFieldOptions(optionsMap)
policySymbols.setRequired(true)
policySymbols.setHidden(false)
}
else if (areaVal == "Personal Lines") {
def optionsMap = options.findAll {
it.value in ["PL-AHP", "PL-AR", "PL-DF", "PL-FA", "PL-FP", "PL-GH", "PL-HM", "PL-HO", "PL-IM", "PL-MA", "PL-MC", "PL-MH", "PL-NSA", "PL-PA", "PL-PA3", "PL-PLH", "PL-PPT", "PL-PRH", "PL-SH", "PL-TH"]
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}

policySymbols.setFieldOptions(optionsMap)
policySymbols.setRequired(true)
policySymbols.setHidden(false)
}

else if (areaVal == "N/A") {
def optionsMap = options.findAll {
it.value in ["N/A"]
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}

policySymbols.setFieldOptions(optionsMap)
policySymbols.setRequired(false)
policySymbols.setHidden(true)
}

 

2 answers

0 votes
Mark Markov Community Leader Jul 12, 2018

Hello @Scott Boisvert

Try to add some log messages to detect problem. 

And see altlassian-jira.log when you reproduce the case.

It will give you a direction.

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def policySymbols = getFieldByName("Policy Symbols")
def area = getFieldByName("Area")

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

def areaVal = area.getValue()
log.error("AreaVal: " + areaVal)

if (areaVal == "All") {
def optionsMap = options.findAll {
it.value in ["CL-BP", "CL-CA", "CL-CPP", "CL-CT", "CL-CUP", "CL-DO", "CL-EO", "CL-EPL", "CL-F", "CL-FO", "CL-FUP", "CL-GLA", "CL-IMP", "CL-OE", "CL-SCP", "CL-WCP", "CL-XA", "CL-XMP", "CL-XPP", "PL-AHP", "PL-AR", "PL-DF", "PL-FA", "PL-FP", "PL-GH", "PL-HM", "PL-HO", "PL-IM", "PL-MA", "PL-MC", "PL-MH", "PL-NSA", "PL-PA", "PL-PA3", "PL-PLH", "PL-PPT", "PL-PRH", "PL-SH", "PL-TH"]
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}

policySymbols.setFieldOptions(optionsMap)
policySymbols.setRequired(true)
policySymbols.setHidden(false)
}
else if (areaVal == "Commercial Lines") {
def optionsMap = options.findAll {
it.value in ["CL-BP", "CL-CA", "CL-CPP", "CL-CT", "CL-CUP", "CL-DO", "CL-EO", "CL-EPL", "CL-F", "CL-FO", "CL-FUP", "CL-GLA", "CL-IMP", "CL-OE", "CL-SCP", "CL-WCP", "CL-XA", "CL-XMP", "CL-XPP"]
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}

policySymbols.setFieldOptions(optionsMap)
policySymbols.setRequired(true)
policySymbols.setHidden(false)
}
else if (areaVal == "Personal Lines") {
def optionsMap = options.findAll {
it.value in ["PL-AHP", "PL-AR", "PL-DF", "PL-FA", "PL-FP", "PL-GH", "PL-HM", "PL-HO", "PL-IM", "PL-MA", "PL-MC", "PL-MH", "PL-NSA", "PL-PA", "PL-PA3", "PL-PLH", "PL-PPT", "PL-PRH", "PL-SH", "PL-TH"]
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}

policySymbols.setFieldOptions(optionsMap)
policySymbols.setRequired(true)
policySymbols.setHidden(false)
}

else if (areaVal == "N/A") {
def optionsMap = options.findAll {
it.value in ["N/A"]
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}
log.error("Ooptions fo N/A: "+ optionsMap.toString())
policySymbols.setFieldOptions(optionsMap)
policySymbols.setRequired(false)
policySymbols.setHidden(true)
}

@Mark Markov

Thanks, unfortunately adding the logs didn't produce anything to lead me to think something is wrong with the code. All three actions returned nothing. I found a nicer way to set the checkboxes as well. Still having the same issue though.

 

log.error ("Set selections to N/A only: " + policySymbols.setFieldOptions(options.findAll {it.value in ["N/A"]}).toString())
log.error("Set Policy Symbol field not Required: " + policySymbols.setRequired(false).toString())
log.error("Hide Policy Symbol Field: " + policySymbols.setHidden(true).toString())

2018-07-12 14:50:10,287 http-nio-8080-exec-6 ERROR boisvers 890x28122x1 1ul55mf 192.168.108.85 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.j.groovy.user.FieldBehaviours] Set selections to N/A only: Form field ID: customfield_10408, value: 2018-07-12 14:50:10,287 http-nio-8080-exec-6 ERROR boisvers 890x28122x1 1ul55mf 192.168.108.85 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.j.groovy.user.FieldBehaviours] Set Policy Symbol field not Required: Form field ID: customfield_10408, value: 2018-07-12 14:50:10,288 http-nio-8080-exec-6 ERROR boisvers 890x28122x1 1ul55mf 192.168.108.85 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.j.groovy.user.FieldBehaviours] Hide Policy Symbol Field: Form field ID: customfield_10408, value:

 

@Mark Markov

Logs didn't call anything out that I could see. I did change the setting of the values to something a littler cleaner, but still have the issue with the "setHidden" command not working.

log.error ("Set selections to N/A only: " + policySymbols.setFieldOptions(options.findAll {it.value in ["N/A"]}).toString())
log.error("Set Policy Symbol field not Required: " + policySymbols.setRequired(false).toString())
log.error("Hide Policy Symbol Field: " + policySymbols.setHidden(true).toString())

2018-07-12 14:50:10,287 http-nio-8080-exec-6 ERROR boisvers 890x28122x1 1ul55mf 192.168.108.85 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.j.groovy.user.FieldBehaviours] Set selections to N/A only: Form field ID: customfield_10408, value: 
2018-07-12 14:50:10,287 http-nio-8080-exec-6 ERROR boisvers 890x28122x1 1ul55mf 192.168.108.85 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.j.groovy.user.FieldBehaviours] Set Policy Symbol field not Required: Form field ID: customfield_10408, value: 
2018-07-12 14:50:10,288 http-nio-8080-exec-6 ERROR boisvers 890x28122x1 1ul55mf 192.168.108.85 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.j.groovy.user.FieldBehaviours] Hide Policy Symbol Field: Form field ID: customfield_10408, value: 

@Scott Boisvert  I'm having the same problem. setHidden and setFieldOptions mess up each other. Were you able to find a solution?

@Merielle Impreso I did actually. Though I don't remember what it was I was able to talk our folks out of using those fields so I removed the script.

If I remember correctly though, it had something to do with where the setHidden call was placed in the script. I think by moving it around it fixed it.

@Scott Boisvert Thank you for responding. I actually find a workaround on this. What I want is that:

  1. I have two fields: Category (selection) and Subcategory (checkboxes).
  2. The options in the Subcategory would depend on the selection of the Category.
  3. So initially, if no Category is selected, Subcategory should be hidden. 

However, the setHidden and setFieldOptions messed up. I tried what you suggested and experiment on the positioning of these functions, but in vain. Anyway, what I did is to have a default Category and make it a required field (since it is really required in our form). That way, there is no None option in the Category selection.

@Merielle Impreso 

I believe that is how I got around it as well. The field was required and set the default to include all options, then updated the checkboxes as necessary.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

365 views 1 3
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you