Click a check box and display other fields within the screen Edited

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
Jenna Davis Community Champion Jun 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

 

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 Community Champion Jul 05, 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

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 Community Champion Jul 05, 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 

 

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 Community Champion Jul 06, 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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

128 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you