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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

Behavior to hide 30-40 custom fields

I am creating a script impacting 30-40 custom fields. Where the fields to display depends upon a custom field. For all the different combinations, I have to make a set of fields available and required. i.e.

If the trigger is set to India: only make fieldA, fieldB, and fieldC available and required.
else if the trigger is set to US: only make fieldB, fieldE, and fieldF available and required. 

case "Create new chair":        
dynField1.setHidden(
false);        
dynField1.setRequired(
true);
dynField2.setHidden(false);
dynField2.setRequired(true);        
dynField3.setHidden(
false);        
dynField3.setRequired(
true);        
break    
case "Create new table":
dynField4.setHidden(false);        
dynField4.setRequired(
true);
dynField3.setHidden(false);        
dynField3.setRequired(true);
        
dynField5.setHidden(
false);        
dynField5.setRequired(
true);
break

There are more than 15 such cases. Additionally, with the change in the trigger field, all the non-relevant custom fields should be marked as hidden and optional.

As you see above, this code isn't the best looking or efficient codes. Is there a better way of doing this?

 

Regards
Upasna Bassi

1 answer

1 accepted

0 votes
Answer accepted

Put the option outside of the logic in a data structure of some kind.

Depending on the nature of the permutations of which fields are visible for which option and what your default state should be, you can try something like (assumes fields are hidden unless specified and all visible fields are required)

def fieldName_VisOptionsMap = [ 
fieldNameA:[option1, option2],
fieldNameB: [option2, option2],
//... etc
]

def selectedOption = getFieldByName(optionFieldName).value

fieldName_VisOptionsMap .each{fieldName, visibleOptions ->
def visible = visibleOptions.contains(selectedOption)
getFieldByName(fieldName).setHidden(!visible).setRequired(visible)
}

 If the permutations are more complex, you can make your structure more complex

 

def optionMaps = [
[
fieldsNames: [fieldA, fieldB, fieldD],
visibleWhen: [option1, option2],
requiredWhen: [option2]
],
[
fieldNames: [fieldC, fieldE],
visiblewhen: [option2],
requiredWhen: [option2]
]
// ... etc
]
def selectedOption = getFieldByName(optionFieldName).value

optionMaps.each{optionMap->
def visible = optionMap.visibleWhen.contains(selectedOption)
def required = optionMap.requiredWhen.contains(selectedOption)
fieldNames.each{fieldName->
getFieldByName(fieldName).setHidden(!visible).setRequired(required)
}
}

The possibilities are only limited by your imagination. Just match the logic to the structure. and vice versa.

Thankyou Peter-Dave! This is really helpful. I was able to implement the solution to my problem with a map structure.

Suggest an answer

Log in or Sign up to answer
TAGS

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