With Scriptrunner Behaviour - hide a date field based on the another date field
Asked by Patricia Vale:

Hello,

I want a date field to only be visible when another field (Date and Time occurrence) is filled in.

I would like to implement this behavior with the script runner.

Thankful if you can help me.

Thanks.


Answers:
author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Patricia Vale](/t5/user/viewprofilepage/user-id/4184250)

Your requirement is pretty straightforward.

You will need to create a Server-Side Behaviour for the first Date field, i.e. when the value is set in this field, the second Date field will be hidden.

When initialising the field the Server-Side Behaviour is configured for, you must declare the field using this structure:-

```
def field = getFieldById(fieldChanged)
```

Otherwise, the Behaviour will not work as expected.

Below is a sample working code for your reference:-  

```
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def date1 = getFieldById(fieldChanged)
def date1Value = date1.value

def date2 = getFieldByName('Date 2')
date2.hidden = false

if (date1Value) {
    date2.hidden = true
}
```

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Server-Side Behaviour configuration:-

![behaviour_config.png](https://community.atlassian.com/t5/image/serverpage/image-id/295864iF7102AA10FF91494/image-size/large?v=v2&px=999 "behaviour_config.png")

Below are a couple of test screenshots for your reference:-

1) When the create screen is first opened, both the date fields are visible as no modification has been set to the first Date field as shown in the screenshot below:-

![test1.png](https://community.atlassian.com/t5/image/serverpage/image-id/295867iB509FC5DEF8F28F3/image-size/large?v=v2&px=999 "test1.png")

2) Next, add a value to the first date field as shown in the screenshot below:-

![test2.png](https://community.atlassian.com/t5/image/serverpage/image-id/295865i99854750EC60CF18/image-size/large?v=v2&px=999 "test2.png")

3) As expected, once the value is set in the first date field, the second date field is hidden, as shown in the screenshot below:-

![test3.png](https://community.atlassian.com/t5/image/serverpage/image-id/295866iA60CBD15738AECD2/image-size/large?v=v2&px=999 "test3.png")

I hope this helps to solve your question. :-)  

Thank you and Kind regards,

Ram


author: Joseph Chung Yin
body: [@Patricia Vale](/t5/user/viewprofilepage/user-id/4184250) -

Hi Patricia:

Here are the steps and the "server side script" for you to try

1) Create a Behavior setup for your target project and specific issue type or request type

2) In the setup, select the field (the field that is filled or not)

3) Add the server side script to something like below  

```
def issueXField = getFieldById("customfield_xxxxx")     // your date field

def issueYField = getFieldById("customfield_xxxxx")   // your field you want to hide/display

String issueXValue = issueXField.getValue()

if (!issueXValue){  // if the date field is not empty

        issueYField.setHidden(false)

} else {

  issueYField.setHidden(true)    

}
```

Hope this helps. If it does, please remember to click the "Accept answer" button, so others with similar asks will see this posting as an answered entry.

Best, Joseph Chung Yin

Jira/JSM Functional Lead, Global Technology Applications Team

Viasat Inc.

comments: author: Patricia Vale
body: Hi [@Joseph Chung Yin](/t5/user/viewprofilepage/user-id/396739) ,

Thank you for your quick response and help.

I tried, but it didn't work.

Currently we are in Data Center and not Server. Could that be why?

Thanks


author: Joseph Chung Yin
body: [@Patricia Vale](/t5/user/viewprofilepage/user-id/4184250) -

No, it should not matter. Please provide more details on your Behavior configuration setup details...

Lastly what is your DC version and ScriptRunner for Jira version?

Best, Joseph


Tags: behaviours

jira-service-management

scriptrunner