Click a check box and display other fields within the screen

Andrew Lonardelli June 30, 2017

Hi everyone,

Using the behaviours initializer on Scriptrunner, I am trying to display certain fields only when a user clicks on a certain check box. I am not sure how exactly to start the groovy script and I am wondering if anybody has seen something similar or has done something similar that they are willing to share. 

 

Example: User clicks on single day check box and then full day and half day check boxes display along with a date picker. 

1 answer

1 accepted

1 vote
Answer accepted
Jenna Davis
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.
June 30, 2017

Hello,

You should be able to use something similar to what is shown in this example in the docs.

Let me know if you need help getting it set up.

Jenna

 

Andrew Lonardelli July 5, 2017

Thanks for the reply Jenna. The field doesn't change when the custom field either gets chosen or clicked. I also used another field besides a checkbox to see if that would work. Some pictures and code can be seen below:Screenshot (145).pngScreenshot (146).png

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
 
 

FormField ParentField = getFieldById(getFieldChanged())   
FormField ChildField1 = getFieldByName ("Day Duration")
String ParentFieldVal = (String) ParentField.getValue()
if (ParentFieldVal == ("Single Day")) {
ChildField1.setHidden(false)
ChildField1.setRequired(true)
}
else {
ChildField1.setHidden(true)
ChildField1.setRequired(false)
}
Jenna Davis
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.
July 5, 2017

Hello,

You should make an initializer function containing this:

def hiddenField = getFieldByName("the field you want initially hidden")
hiddenField.setHidden(true)

And a behavior function that acts on the check box field containing something similar to this: 

def hiddenField = getFieldByName("the initially hidden field")
def checkBox = getFieldById(getFieldChanged())

def selectedOption = checkBox.getValue() as String
def isOtherSelected = selectedOption == "the checked option(s) that makes your field appear"

hiddenField.setHidden(!isOtherSelected)
hiddenField.setRequired(isOtherSelected)

You may have to fiddle with with what selectionOption ends up equaling to make it true if you want to use more than one check box in order to make the field appear. I tested this using just one checkbox. This should get you started at least though. 

Let me know if you have any more questions!

Jenna

Like # people like this
Andrew Lonardelli July 5, 2017

I am sorry, I don't understand the differences between the initializer function and the behanviour function. I am new to working with Behaviour functions.

Jenna Davis
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.
July 5, 2017

No problem!

You'll want to create all of this under a single 'Behavior'. However, you want to set up the first part under an initialiser function. 

It will look something like this:

Screen Shot 2017-07-05 at 3.57.36 PM.png

Click 'Create Initialiser' and insert the snippet I provided. 

Then, the field behavior is set up by picking your field (the check box) out of this list:

Screen Shot 2017-07-05 at 3.57.53 PM.pngYou will then see something like this appear:

Screen Shot 2017-07-05 at 3.58.12 PM.pngClick 'Add serverside script' here and add the next part of the code.

When you're done, the entire Behavior should look something like this:

Screen Shot 2017-07-05 at 3.56.13 PM.pngLet me know if you need any more help!

Jenna 

 

Like vrgl2 likes this
Andrew Lonardelli July 6, 2017

Hey Jenna, this has been really helpfull. I got it to work.

I have one more question, would this method be able to display a scripted field as a view only in the create window? (Kind of like a dynamic field)

Jenna Davis
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.
July 6, 2017

Good! I'm glad I could help you out. :)

Scripted Fields don't show up in the create or update/edit screens.

By a 'dynamic field' do you mean that if you do something in one field the content changes in your read-only field? If so, that would probably be best achieved by another behavior. Just make sure you don't have mutiple behavior functions acting on one field. That can cause unpredictable behavior or make them simply not work. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events