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

Selecting Checkbox field option to show Date Field

Priyanka khare October 20, 2020


Hello ,

I am using the below code to Show and Hide the Target Date Field in my JIRA Server instance using scriptrunner for JIRA . This Target Date will be visible only if option C is selected on the checkbox field -
I have used behaviours and added the checkbox field and written the following code as rhe server side script-
/////////////////////////
def changedCF = getFieldById(getFieldChanged()) // Checkbox A
def custField1 = getFieldById("customfield_11004") // Date Field

boolean isCollection = changedCF.getValue() instanceof Collection

if (isCollection) {

def changedCFValue = changedCF.getValue() as List

if (changedCFValue.contains("A")||changedCFValue.contains("B")||changedCFValue.contains("D")||changedCFValue.contains("E")||changedCFValue.contains("F"))
{
custField1.setHidden(true)
custField1.setFormValue(null)

} else {
custField1.setHidden(false)
}


} else {

def changedCFValue = changedCF.getValue() as String

if (changedCFValue.contains("C")) {
custField1.setHidden(false)
custField1.setFormValue(null)

} else {
custField1.setHidden(true)

}

}
///////////////////
This is not working as per expectation, Can someone please help or direct me to achieve my requirement?


Regards
Priyanka

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Jeroen Poismans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 22, 2020

Hi Priyanka,

I can see why this isn't working. When no option is selected the script might fail. I got it working with fewer code.

First, in your behaviour you should add an initialiser script (at the the top) that sets the default values in load of the Creat screen. Here is the initializer:

def custField1 = getFieldById("customfield_11004") // Date Field
custField1.setHidden(true)
custField1.setFormValue(null)

 

Then in the field code (which gets executed everytime you change the checkboxes):

def changedCF = getFieldById(getFieldChanged()) // Checkbox A
def custField1 = getFieldById("customfield_11004") // Date Field

def changedCFValue = changedCF.getValue()

if (changedCFValue == null || !changedCFValue.contains("C")) {
custField1.setHidden(true)
} else {
custField1.setHidden(false)
custField1.setFormValue(null)
}

Notice how in the "if" statement there is a null check added. This should now work with no options selected, 1 option, multiple options.

 

Let me know if this solved it!

Jeroen

TAGS
AUG Leaders

Atlassian Community Events