Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Show/Hide fields based on date Field "Due Date"

Lakshmi CH
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.
January 12, 2024

 

Hi Team, 

I am trying to create a script that shows or hides a single line text field called "Reason for the Due Date" based on the value of a date field called "Due Date". Although the script is working, the "Reason for the Due Date" field is visible when the ticket is initially created, even when the "Due Date" field is blank. Ideally, the "Reason for the Due Date" field should only be visible when a value is entered in the "Due Date" field and should disappear when the "Due Date" field is blank.

On Create :

date_field.png

 

import com.onresolve.jira.groovy.user.FieldBehaviours

import org.apache.log4j.Logger

import org.apache.log4j.Level

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def log = Logger.getLogger(getClass())

// Set log level

log.setLevel(Level.DEBUG)

def dueField = getFieldByName("Due Date")

def reasonField = getFieldByName('Reason for the Due Date')

def dateFieldValue = dueField.getValue() as Date

if (dateFieldValue == null)

{

   

    reasonField.setHidden(true)

    reasonField.setRequired(false)

}

else

{

   reasonField.setHidden(false)

   reasonField.setRequired(true)

}

1 answer

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
January 12, 2024 edited

Hi @Lakshmi CH

It appears that your code is faulty. 

Also, for your code to work, you must use the Server-Side Behaviour for the Date Field.

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.toString()
def sampleText = getFieldByName('Sample Text')

sampleText.required = false
sampleText.hidden = true

if (date1Value) {
sampleText.hidden = false
sampleText.required = 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 Behaviour configuration for your reference:-

behaviour_config.png

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

1. The Date field is empty by default when the Create screen first appears. Hence, the Text Field is set to not required and is hidden.

test1.png

2. When the value is set for the Date field, as expected, the Text Field is now visible and is set to required, as shown in the screenshot below. If the Date field is cleared, the Text Field will not be set to required and hidden as expected.

test2.png

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

Thank you and Kind regards,

Ram

Lakshmi CH
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.
January 15, 2024

Thank you Ram @Ram Kumar Aravindakshan _Adaptavist_ . Its working expected,

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
January 15, 2024

Hi @Lakshmi CH

Glad to hear the solution worked for you. :-)

Please accept the answer.

Thank you and Kind regards,

Ram

Sangram Darole February 23, 2024 edited

Hello @Ram Kumar Aravindakshan _Adaptavist_ 

I am trying to introduce similar automation to make sure the Due date Change Reason (multiline text) is mandatory when the Due Date field (date type) changes its value.

When the field changes from BLANK to Value no need to populate the field.

am not a hardcore developer, took the reference of your code. log says no failure in executions.this is at the issue creation stage

image.png

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

@BaseScript FieldBehaviours behaviours
def Date = getFieldById(fieldChanged)
def DateValue = Date.value.toString()
def Reason  = getFieldByName('Live Date Change Reason')

 Reason.required = false
 Reason.hidden = true

if (DateValue) {
    Reason.hidden = false
    Reason.required = true
}
Your help is much appreciated. Thanks 
The script is working now but the requirement is whenever at any stage the Live date changes Live date change reason should be mandatory for the users every time. 

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, confluence, loom, atlassian intelligence, ai notes, ai-powered meeting notes, atlassian community events, ace, confluence ai, loom ai integration, ai note-taking, atlassian ai features, team '25, atlassian live learning, confluence automation

Unlock AI-powered meeting notes: Join our live learning session! 📹

Did you catch the news at Team ‘25? With Loom, Confluence, Atlassian Intelligence, & even Jira 👀, you won’t have to worry about taking meeting notes again… unless you want to. Join us to explore the beta & discover a new way to boost meeting productivity.

Register today!
AUG Leaders

Atlassian Community Events