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

Remove required field based on other field value

Valiantys Support August 12, 2022

Hi All,

I'm looking to see if my script can be improved for the following needs, I have a single choice drop down (yes, no). When 'No' is selected, I need to remove all of the required fields listed in the script. 

When I first created this, it worked, but for some odd reason it's no longer working.  Could someone look over my script and tell me if there is something that sounds out as not functioning?

Thanks!



def textField = getFieldByName("File Submitted Date")
def textField1 = getFieldByName("Requested Date of Transfer")
def textField2 = getFieldByName("Media to Client")
def textField3 = getFieldByName("Tran 119")
def textField4 = getFieldByName("Is a TLP required")
def textField5 = getFieldByName("Should TLP fees be billed to the client")
def textField6 = getFieldByName("Does the client need Tax amounts for these loans?")
def textField7 = getFieldByName("Change Contract Service_Type")
def textField8 = getFieldByName("Do you need TPS Recon?")
def textField9 = getFieldByName("Do you need a WI options listing")
def textField10 = getFieldByName("Is a bene fee to be charged")
def textField11 = getFieldByName("Fee Type(s) Change Needed?")
def selectList = getFieldByName("Will there be a BENE final?")

def selectListValue = selectList.getValue()

if (selectListValue == "No") {
textField.setRequired(false)
textField1.setRequired(false)
textField2.setRequired(false)
textField3.setRequired(false)
textField4.setRequired(false)
textField5.setRequired(false)
textField6.setRequired(false)
textField7.setRequired(false)
textField8.setRequired(false)
textField9.setRequired(false)
textField10.setRequired(false)
textField11.setRequired(false)
} else {
textField.setRequired(true)
textField1.setRequired(true)
textField2.setRequired(true)
textField3.setRequired(true)
textField4.setRequired(true)
textField5.setRequired(true)
textField6.setRequired(true)
textField7.setRequired(true)
textField8.setRequired(true)
textField9.setRequired(true)
textField10.setRequired(true)
textField11.setRequired(true)
}

1 answer

Suggest an answer

Log in or Sign up to answer
1 vote
Peter-Dave Sheehan
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.
August 12, 2022

How about something like this:

def fieldList = [
'File Submitted Date',
'Requested Date of Transfer',
'Media to Client',
'Tran 119',
'Is a TLP required',
'Should TLP fees be billed to the client',
'Does the client need Tax amounts for these loans?',
'Change Contract Service_Type',
'Do you need TPS Recon?',
'Do you need a WI options listing',
'Is a bene fee to be charged',
'Fee Type(s) Change Needed?'
]
def selectList = getFieldByName("Will there be a BENE final?")
def selectListValue = selectList.getValue()

fieldList.each { fieldName ->
def hide = selectListValue == "No"
getFieldByName(fieldName).setHidden(hide).setRequired(!hide)
}
Valiantys Support August 12, 2022

Thank you @Peter-Dave Sheehan 

I'll give it a try and see if it works for me. I'll report back on the status. 

Valiantys Support August 26, 2022

Hi @Peter-Dave Sheehan 

Any idea as to why it's causing an error? When the option "No" is selected, it's not removing the required function from those fields

Error

There are fields that are required or in an error state, but the administrator has hidden them or made them readonly using a behaviour.

Thanks!

Norm

Peter-Dave Sheehan
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.
August 26, 2022

For fields to be conditionally required using behaviour, they can't be required in other configurations.

Check the field configuration scheme and make sure the field is optional there. 

Note that this is one of the downsides of behaviour. It only works on screens and requires the underlying API to open. So if you do this, the screen may show the field as required, but someone interacting with the API will be able to submit issues without that field. On the other hand, using the field config scheme to make a field required will also make it required via the api.

Valiantys Support August 29, 2022

I see, currently the fields are set to required in the field configuration, would I need to remove the required fields from the field configuration so the behaviour you provided enables it properly?

Peter-Dave Sheehan
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.
August 29, 2022

Correct. The field must be optional in the field configuration if you want to use behaviour to conditionally make it visible/required or hidden/optional.

TAGS
AUG Leaders

Atlassian Community Events