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

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

Show/Hide fields deppending on Value

Hello.

I'm trying to build a behavior in order to show just the fields I need to see depending on the value of a previous field. We have 3 "sets" of fields, but with the script I'm using, it only works for the first set (SN), and I need it to apply to all 3 sets correctly.

Here's my script:

def Field1 = getFieldByName("SN (1)")
def Field2 = getFieldByName("SN (2)")
def Field3 = getFieldByName("SN (3)")
def Field4 = getFieldByName("SN (4)")
def Field5 = getFieldByName("SN (5)")
def Field11 = getFieldByName("SN (6)")
def Field12 = getFieldByName("SN (7)")
def Field13 = getFieldByName("SN (8)")
def Field14 = getFieldByName("SN (9)")
def Field15 = getFieldByName("SN (10)")
def Field6 = getFieldByName("New Net price (1)")
def Field7 = getFieldByName("New Net price (2)")
def Field8 = getFieldByName("New Net price (3)")
def Field9 = getFieldByName("New Net price (4)")
def Field10 = getFieldByName("New Net price (5)")
def Field16 = getFieldByName("New Net price (6)")
def Field17 = getFieldByName("New Net price (7)")
def Field18 = getFieldByName("New Net price (8)")
def Field19 = getFieldByName("New Net price (9)")
def Field20 = getFieldByName("New Net price (10)")
def Field21 = getFieldByName("Current Net price (1)")
def Field22 = getFieldByName("Current Net price (2)")
def Field23 = getFieldByName("Current Net price (3)")
def Field24 = getFieldByName("Current Net price (4)")
def Field25 = getFieldByName("Current Net price (5)")
def Field26 = getFieldByName("Current Net price (6)")
def Field27 = getFieldByName("Current Net price (7)")
def Field28 = getFieldByName("Current Net price (8)")
def Field29 = getFieldByName("Current Net price (9)")
def Field30 = getFieldByName("Current Net price (10)")

def NumberField = getFieldById(getFieldChanged())

def selectedOption = NumberField.getValue() as String
def Option1 = selectedOption == "1"
def Option2 = selectedOption == "2"
def Option3 = selectedOption == "3"
def Option4 = selectedOption == "4"
def Option5 = selectedOption == "5"
def Option6 = selectedOption == "6"
def Option7 = selectedOption == "7"
def Option8 = selectedOption == "8"
def Option9 = selectedOption == "9"
def Option10 = selectedOption == "10"

Field1.setHidden !(Option1 || Option2 || Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field1.setRequired (Option1 || Option2 || Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field2.setHidden !(Option2 || Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field2.setRequired (Option2 || Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field3.setHidden !(Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field3.setRequired (Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field4.setHidden !(Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field4.setRequired (Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field5.setHidden !(Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field5.setRequired (Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field11.setHidden !(Option6 || Option7 || Option8 || Option9 || Option10)
Field11.setRequired (Option6 || Option7 || Option8 || Option9 || Option10)
Field12.setHidden !(Option7 || Option8 || Option9 || Option10)
Field12.setRequired (Option7 || Option8 || Option9 || Option10)
Field13.setHidden !(Option8 || Option9 || Option10)
Field13.setRequired (Option8 || Option9 || Option10)
Field14.setHidden !(Option9 || Option10)
Field14.setRequired (Option9 || Option10)
Field15.setHidden !(Option10)
Field15.setRequired (Option10)

Field6.setHidden !(Option1 || Option2 || Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field6.setRequired (Option1 || Option2 || Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field7.setHidden !(Option2 || Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field7.setRequired (Option2 || Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field8.setHidden !(Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field8.setRequired (Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field9.setHidden !(Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field9.setRequired (Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field10.setHidden !(Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field10.setRequired (Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field16.setHidden !(Option6 || Option7 || Option8 || Option9 || Option10)
Field16.setRequired (Option6 || Option7 || Option8 || Option9 || Option10)
Field17.setHidden !(Option7 || Option8 || Option9 || Option10)
Field17.setRequired (Option7 || Option8 || Option9 || Option10)
Field18.setHidden !(Option8 || Option9 || Option10)
Field18.setRequired (Option8 || Option9 || Option10)
Field19.setHidden !(Option9 || Option10)
Field19.setRequired (Option9 || Option10)
Field20.setHidden !(Option10)
Field20.setRequired (Option10)

Field21.setHidden !(Option1 || Option2 || Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field21.setRequired (Option1 || Option2 || Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field22.setHidden !(Option2 || Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field22.setRequired (Option2 || Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field23.setHidden !(Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field23.setRequired (Option3 || Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field24.setHidden !(Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field24.setRequired (Option4 || Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field25.setHidden !(Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field25.setRequired (Option5 || Option6 || Option7 || Option8 || Option9 || Option10)
Field26.setHidden !(Option6 || Option7 || Option8 || Option9 || Option10)
Field26.setRequired (Option6 || Option7 || Option8 || Option9 || Option10)
Field27.setHidden !(Option7 || Option8 || Option9 || Option10)
Field27.setRequired (Option7 || Option8 || Option9 || Option10)
Field28.setHidden !(Option8 || Option9 || Option10)
Field28.setRequired (Option8 || Option9 || Option10)
Field29.setHidden !(Option9 || Option10)
Field29.setRequired (Option9 || Option10)
Field30.setHidden !(Option10)
Field30.setRequired (Option10)

 

Any idea or suggestion on hoe to make it work properly or improve it?

KR

1 answer

1 accepted

0 votes
Answer accepted

For one... let's start by simplifying

Start by abstracting your field option from your action logic (think about being able to easily modify the fields or options. Then just iterate through and take the same action on each option.

def fieldOptionMap= [
   "SN (1)":["1","2","3","4","5","6","7","8","9","10"],
   "SN (2)":["2","3","4","5","6","7","8","9","10"],
   "SN (3)":["3","4","5","6","7","8","9","10"],
   "SN (4)":["4","5","6","7","8","9","10"],
   "SN (5)":["5","6","7","8","9","10"],
   "SN (6)":["6","7","8","9","10"],
   "SN (7)":["7","8","9","10"],
   "SN (8)":["8","9","10"],
   "SN (9)":["9","10"],
   "SN (10)":["10"],
   "New Net price (1)":["1","2","3","4","5","6","7","8","9","10"],
   "New Net price (2)":["2","3","4","5","6","7","8","9","10"],
   "New Net price (3)":["3","4","5","6","7","8","9","10"],
   "New Net price (4)":["4","5","6","7","8","9","10"],
   "New Net price (5)":["5","6","7","8","9","10"],
   "New Net price (6)":["6","7","8","9","10"],
   "New Net price (7)":["7","8","9","10"],
   "New Net price (8)":["8","9","10"],
   "New Net price (9)":["9","10"],
   "New Net price (10)":["10"],
   "Current Net price (1)":["1","2","3","4","5","6","7","8","9","10"],
   "Current Net price (2)":["2","3","4","5","6","7","8","9","10"],
   "Current Net price (3)":["3","4","5","6","7","8","9","10"],
   "Current Net price (4)":["4","5","6","7","8","9","10"],
   "Current Net price (5)":["5","6","7","8","9","10"],
   "Current Net price (6)":["6","7","8","9","10"],
   "Current Net price (7)":["7","8","9","10"],
   "Current Net price (8)":["8","9","10"],
   "Current Net price (9)":["9","10"],
   "Current Net price (10)":["10"]
]

def NumberField = getFieldById(getFieldChanged())
def selectedOption = NumberField.value.value

fieldOptionMap.each{ fieldName, optionValues ->
   def formField = getFieldByName(fieldName)
   formfield.setHidden(!(selectedOption in optionValues ) )
   formfield.setRequired(selectedOption in optionValues )

}

Worked great, thank you so much!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across their  Jira Cloud instance.  ...

694 views 17 19
Read article

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