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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.