With Scriptrunner Behaviour - hide a date field based on the another date field

Patricia Vale December 7, 2023

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.

3 answers

2 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 7, 2023

Hi @Patricia Vale

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

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

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

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

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

Thank you and Kind regards,

Ram

0 votes
Answer accepted
Joseph Chung Yin
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 7, 2023

@Patricia Vale -

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.

Patricia Vale December 7, 2023

Hi @Joseph Chung Yin ,

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

Joseph Chung Yin
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 7, 2023

@Patricia Vale -

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

0 votes
Sheila S_
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 7, 2024

@Joseph Chung Yin  - I am wondering if you can help me out.  I have a Behavior setup to work properly on the Create and Edit form, but the field still shows up on the IssueView.  The behavior shows one field if the content of another field is a certain value. 

I saw a video for Jira Cloud where there is a setting to choose which views to use the behavior on, but I do not see that option in Jira DC (Version 8.3.4.0).  Is there a way to set that in the script itself?

Joseph Chung Yin
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 8, 2024

@Sheila S_ 

Welcome to the community.  In your ScriptRunner for Jira add-on installation, can you confirm that you also see the Behavior component call out.

2024-11-08_09-35-42.png

Best, Joseph

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events