Script Runner Behaviour: Show Fields Based on Option/s Picked in Checkbox Field

Josh Knutkowski September 15, 2016

Hello Everyone,

I've got a checkbox field with the following info:

  • Name: Request Type (customfield_10180)
  • Three options:
    • Corp Comms
    • Design
    • Marketing

I have a set of 15 fields, and I need the following to happen:

  • If I choose one checkbox then 5 fields should show up
  • If I click two check-boxes 10 fields show up
  • If I click 3 check-boxes then 15 field show up

So, if one checkbox is selected, then if I select another checkbox I need it to keep the first 5 additional fields presented from the first checkbox, plus present the 5 additional fields that should go with the second selected checkbox, and again for a third checkbox.

Though I haven't gotten this far with the code yet, I also need it reverse the process, where if I deselect a checkbox, the additional fields go away that were presented when the check-box was selected.

Here's the server-side script as it stands now:

def dropDown = getFieldById("customfield_10180")
def conditionA = getFieldById("customfield_10012")
def conditionB = getFieldById("customfield_10004")
def conditionC = getFieldById("customfield_10013")
def conditionD = getFieldById("customfield_10014")
def conditionE = getFieldById("customfield_10017")
def conditionF = getFieldById("customfield_10018")
def conditionG = getFieldById("customfield_10019")
def conditionH = getFieldById("customfield_10020")
def conditionI = getFieldById("customfield_10155")
def conditionJ = getFieldById("customfield_10021")
def conditionK = getFieldById("customfield_10022")
def conditionL = getFieldById("customfield_10023")
def conditionM = getFieldById("customfield_10024")
if (dropDown.getValue() == "Corp Comms") {
    conditionA.setHidden(false);
    conditionB.setHidden(false);
    conditionC.setHidden(false);
    conditionD.setHidden(false);
} 
if (dropDown.getValue() == "Design") {
    conditionA.setHidden(false);
    conditionB.setHidden(false);
    conditionC.setHidden(false);
    conditionD.setHidden(false);
    
}
if (dropDown.getValue() == "Marketing") {
    conditionE.setHidden(false);
    conditionF.setHidden(false);
    conditionG.setHidden(false);
    conditionH.setHidden(false);
    conditionI.setHidden(false);
    conditionJ.setHidden(false);
    conditionK.setHidden(false);
    conditionL.setHidden(false);
    conditionM.setHidden(false);
}

Validator Script:

def conditionA = getFieldById("customfield_10012")
def conditionB = getFieldById("customfield_10004")
def conditionC = getFieldById("customfield_10013")
def conditionD = getFieldById("customfield_10014")
def conditionE = getFieldById("customfield_10017")
def conditionF = getFieldById("customfield_10018")
def conditionG = getFieldById("customfield_10019")
def conditionH = getFieldById("customfield_10020")
def conditionI = getFieldById("customfield_10155")
def conditionJ = getFieldById("customfield_10021")
def conditionK = getFieldById("customfield_10022")
def conditionL = getFieldById("customfield_10023")
def conditionM = getFieldById("customfield_10024")
conditionA.setHidden(true);
conditionB.setHidden(true);
conditionC.setHidden(true);
conditionD.setHidden(true);
conditionE.setHidden(true);
conditionF.setHidden(true);
conditionG.setHidden(true);
conditionH.setHidden(true);
conditionI.setHidden(true);
conditionJ.setHidden(true);
conditionK.setHidden(true);
conditionL.setHidden(true);
conditionM.setHidden(true);

Currently, I'm not getting any additional fields presented when I select or deselect the check-boxes. I was able to get fields to appear using almost identical code, the main difference being that the Request Type field was a single-select list field.

I'm using JIRA Core 7.1.9 and Script Runner 4.3.6.

Thank you, and any suggestions would be very welcomed.

1 answer

0 votes
Jonny Carter
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.
September 16, 2016

So, this may simply be an issue of how you've configured your behaviour.

I think that your validator script should probably be an initializer script that's part of your behaviour. However, recall that behaviours can be initialized on both the create and edit forms. So, rather than blanket-hiding all fields, I'd recommend setting them based on current field values.

Then, the first script you list that sets the fields as visible or invisible should be associated with your checkbox field.

https://scriptrunner.adaptavist.com/4.3.6/jira/recipes/behaviours/select-list-other.html details a basic example for marking fields as visible or invisible.

It's not perfectly clear to me how you expect your script to work. That is, do you want fields A-E to be associated with Corp Comms, F-J with Design, and K-O with Marketing? Or is it 1 check mark means A-E are visible, 2 marks means A-J are visible, and 3 check marks means A-O are visible, regardless of which boxes are checked?

In the former case, your code would look something like the following. You can adjust as needed. Take note that you need to use a .contains() style test for a checkbox with multiple options, as noted in the documentation for multi-select lists and checkboxes.

def checkBox = getFieldById("customfield_10180")

def conditionA = getFieldById("customfield_10012")
def conditionB = getFieldById("customfield_10004")
def conditionC = getFieldById("customfield_10013")
def conditionD = getFieldById("customfield_10014")
def conditionE = getFieldById("customfield_10017")
def conditionF = getFieldById("customfield_10018")
def conditionG = getFieldById("customfield_10019")
def conditionH = getFieldById("customfield_10020")
def conditionI = getFieldById("customfield_10155")
def conditionJ = getFieldById("customfield_10021")
def conditionK = getFieldById("customfield_10022")
def conditionL = getFieldById("customfield_10023")
def conditionM = getFieldById("customfield_10024")
def conditionN = getFieldById("customfield_10025")
def conditionO = getFieldById("customfield_10026")

def corpCommsChecked = dropDown.getValue()*.contains("Corp Comms")
conditionA.setHidden(corpCommsChecked)
conditionB.setHidden(corpCommsChecked)
conditionC.setHidden(corpCommsChecked)
conditionD.setHidden(corpCommsChecked)
conditionE.setHidden(corpCommsChecked)

def designChecked = dropDown.getValue()*.contains("Design")
conditionF.setHidden(designChecked)
conditionG.setHidden(designChecked)
conditionH.setHidden(designChecked)
conditionI.setHidden(designChecked)
conditionJ.setHidden(designChecked)

def marketingChecked = dropDown.getValue()*.contains("Marketing")
conditionK.setHidden(marketingChecked)
conditionL.setHidden(marketingChecked)
conditionM.setHidden(marketingChecked)
conditionN.setHidden(marketingChecked)
conditionO.setHidden(marketingChecked)
Josh Knutkowski April 13, 2017

My apologies for the very late response here; the above-provided solution helped provide a basis in getting a Behaviour in place that worked for this scenario.

Thank you for the suggestion, it's much appreciated!

Jennifer H. April 3, 2018

@Josh Knutkowski do you mind sharing your script? I've tried implementing Jonny's script above (modified), but I can't seem to get it to work. I'm running into the exact same problem.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events