Forums

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

How to ensure a field (text field) is filled by users when a date field is altered ?

Aisha M
Contributor
August 19, 2020

I want a text field to capture the reason from users only when an Issue type's Due Date is changed to a latter date,

 

Can I please get some help on how to accomplish this ? Thank you

2 answers

1 accepted

1 vote
Answer accepted
Antoine Berry
Community Champion
August 20, 2020

Hi @Aisha M ,

Please create a behaviours, link it to the due date field and use this script (update the custom field id) : 

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

import java.sql.Timestamp

@BaseScript FieldBehaviours fieldBehaviours

def duedateField = getFieldById("duedate")
def duedate = duedateField.getValue()
def currentDate = new Timestamp(new Date().getTime())

int textFieldId = 10002
def textField = getFieldById("customfield_" + textFieldId)

if (duedate != "" && currentDate > duedate){
textField.setHidden(true)
}
else {
textField.setHidden(false)
}

Let me know if that helped.

Aisha M
Contributor
August 24, 2020

@Antoine Berry Thank you for the reply. How do we link Behavior to the custom fields , is there a specific way ? 

Aisha M
Contributor
August 29, 2020

Hi @Antoine Berry , I did the following thing,

I added the "Reason' custom field to the Edit & View screens of the Epic Issue Type

Then went over to the Behavior Add-on, but the configuration seemed confusing.

Anyway, I added the project & issue type details, and under the "add field" , selected the "Due Date" and added the script & saved.

Later, tried moving the Due Date to a later value, and nothing happened :( Like when I clicked to edit the Due Date, the "Create issue screen' popped open, but nothing prompted me to fill anything, I simply able to modify the field and update the issue with filling the "Reason" text field also.

Aisha M
Contributor
September 6, 2020

@Antoine Berry Hello, are you doing okay ?? :o

Antoine Berry
Community Champion
September 15, 2020

Hi @Aisha M , I am very fine ! I am sorry I was away from office for 3 weeks, much needed holidays. :)

You are doing it correctly, but I noticed I made a mistake in the script above (I was probably thinking about vacations already), so I updated it. Is you select a future date, the text field will appear, otherwise it will be hidden.

Let me know if that helped (if not, take a quick look at the logs).

Aisha M
Contributor
September 15, 2020

@Antoine Berry Wow, that sounds absolutely wonderful !!! I have planned to take off by end of Oct :P Hoping the days run by soon :D

But, I must say you are a wizard with these, coz I had been trying my best to fix the issue but couldn't. lol

Also, should I place the "Reason" text field is all of the "create, edit, view' screens , or just the "edit, view' screens' of the Epic issue type ?

Antoine Berry
Community Champion
September 16, 2020

October will sure be there soon enough !

The script will trigger on create and edit screens, so you should add the Reason field on the create screen if you want users to fill the field when creating the issue.

Aisha M
Contributor
September 16, 2020

@Antoine Berry Makes sense. I ll test it out now with the changes and let you know if I'm able to make it work :)

Like Antoine Berry likes this
Aisha M
Contributor
September 16, 2020

Hello @Antoine Berry , So tried with modified script. 

When I edit the Due Date, the Create issue screen pops, but I am able to select a latter date & update the issue. Nothing prompts me to fill the "Reason" text field, it just sits there.

Also, sometimes, when I edit the Due Date and select a previous date, the Reason field hides, & then when I select a latter date, it shows up  (very good behaviour). But, this doesn't happen all the time. Other times, the field is always there when I edit the Due Date.

Antoine Berry
Community Champion
September 18, 2020

Hi @Aisha M ,

That's exactly what the script I wrote is meant to do. It checks the due date and hides it if it is in the past. It should work on create and edit screens.

Do you need to make it mandatory too ?

Aisha M
Contributor
September 18, 2020

@Antoine Berry  Yes !!! :) I want to make the field mandatory IF the Epic Due Date is modified to a latter date. Is that possible ? 

Like Antoine Berry likes this
Aisha M
Contributor
September 18, 2020

Hello @Antoine Berry Also, one more thing. I meant when the Due Date is changed to a latter value, then the value its currently showing, the text field must be mandatory, and if its moved to a before date than the existing value, it should not be required.

Example - Due Date - 15th Nov

If, Due Date Changed from 15th Nov to 27th Nov (Reason text field mandatory)

If, Due Date changed to 15th Nov to 30th Oct (Reason field to be hidden)

I just noticed that the behavior script, is calculating the date change from the current date. (I could be wrong too)

Like Antoine Berry likes this
Antoine Berry
Community Champion
September 22, 2020

Yes, it is comparing to the current date. I missunderstood then. Here is the script comparing to the current due date and making the field mandatory : 

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

import java.sql.Timestamp

@BaseScript FieldBehaviours fieldBehaviours

if (underlyingIssue){
def duedateField = getFieldById("duedate")
def duedate = duedateField.getValue()

int textFieldId = 10002
def textField = getFieldById("customfield_" + textFieldId)


def dueDate_value = underlyingIssue?.getDueDate()
if (duedate != "" && dueDate_value > duedate){
textField.setHidden(true)
textField.setRequired(false)
}
else {
textField.setHidden(false)
textField.setRequired(true)
}
}

This will not work on create issue screen since the due date has no value then.

Aisha M
Contributor
September 22, 2020

@Antoine Berry  You're the best ! Thank you so much, let me test this :) So should I keep the "Reason" field in the EDIT & VIEW screen alone ? 

Also, just out of curiosity, what would happen if someone simply clears the Due Date with no value & later adds a new value ?

Like Antoine Berry likes this
Aisha M
Contributor
September 22, 2020

@Antoine Berry  It working amazing with the current understanding :) Thank you sooo sooo much ! ^_^

It made the text field mandatory when I changed the Due Date to a latter value, and hid the text field when I selected a earlier date. I also tried completely clearing the Due Date & then it insisted for the text field to be filled.

I hope my manager doesn't add more alterations to this :0 

Like Antoine Berry likes this
Antoine Berry
Community Champion
September 22, 2020

You should keep the "Reason" field on the create issue screen if users might want to fill this field on creation. If not, you can remove it (i guess it makes sense to remove it according to your requirement).

That is true that the user cannot clear the due date field if the reason is not filled. It also seems that if the user fills the due date from an empty due date, the reason is mandatory too.

Is that fine like this ? 

Like Aisha M likes this
Aisha M
Contributor
September 22, 2020

@Antoine Berry I removed it from the Create screen. It made more sense, as they want to capture the reason only when a user delays the Due Date to a latter date.

Would it be possible to avoid the text field if an user clears the Due Date, or if the user fills the due date from an empty due date ?

 

[Also, I'll mark your comment as an answer as the script works as intended for the actual question :):) ]

Antoine Berry
Community Champion
September 22, 2020

Yes, try this script :) 

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

import java.sql.Timestamp

@BaseScript FieldBehaviours fieldBehaviours

if (underlyingIssue){
def duedateField = getFieldById("duedate")
def duedate = duedateField.getValue()

int textFieldId = 10002
def textField = getFieldById("customfield_" + textFieldId)


def dueDate_value = underlyingIssue?.getDueDate()
if (!dueDate_value || duedate == ""){
textField.setHidden(true)
textField.setRequired(false)
}
else if (dueDate_value >= duedate){
textField.setHidden(true)
textField.setRequired(false)
}
else {
textField.setHidden(false)
textField.setRequired(true)
}
}

 

Aisha M
Contributor
September 22, 2020

Okay @Antoine Berry  :) That's amazing ! Will test this out & let you know :) 

Aisha M
Contributor
September 24, 2020

Hi @Antoine Berry  I tested the above script & noticed few things.

I had to change the text field to a drop down field :( . So, I simply changed the field ID alone.

So, the thing I noticed was,

When I completely cleared the Due Date , the reason field doesn't show as mandatory (good).

But, whenever an user randomly edits an existing issue, the "Reason" field is always marked as required, is it possible to make the field work only in relation to the Due Date field (like the Reason field must be mandatory ONLY when the Due is changed to a latter value than the existing value, other time the field can be as it is) ?

Please help :(

Like Antoine Berry likes this
Antoine Berry
Community Champion
September 29, 2020

Hi @Aisha M ,

Sorry if I am not quick to respond, I am in a middle of an urgent/time consuming project. I edited the script just above, I hope it will help. Also make sure the "Reason" field is not mandatory by default in the field configuration.

Aisha M
Contributor
September 29, 2020

@Antoine Berry  I'm so sorry you had to get back just to inform about your schedule . . .  Please take care of the task at your end :):) Hope it goes well. . . . :)

Aisha M
Contributor
September 29, 2020

Hello @Antoine Berry , Just a small clarification, you can respond when you are free :)

I used your below (older) script as the final change, (I was told, "reason" field must be required even if someone completely clears the Due Date)

But only issue is, when a user randomly edits an issue, the "Reason" field becomes mandatory. Is it possible to avoid that alone in the below script :( 

UPDATE - Fixed it luckily by random luck, I used the = symbol to the below script (dueDate_value >= duedate) & now it doesn't require the field when someone is editing an issue. .  Lol . .  I feel like a genius . .  hehehe (was able to figure out only coz of the script you had updated yesterday) THANK YOU SOOOO VERY MUCH FOR YOUR HELP ON THIS !!

 

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

import java.sql.Timestamp

@BaseScript FieldBehaviours fieldBehaviours

if (underlyingIssue){
def duedateField = getFieldById("duedate")
def duedate = duedateField.getValue()

int textFieldId = 10002
def textField = getFieldById("customfield_" + textFieldId)


def dueDate_value = underlyingIssue?.getDueDate()
if (duedate != "" && dueDate_value > duedate){
textField.setHidden(true)
textField.setRequired(false)
}
else {
textField.setHidden(false)
textField.setRequired(true)
}
}

 

Antoine Berry
Community Champion
September 30, 2020

Hi @Aisha M , I love solving issues by doing nothing. :D

Please make sure that all your cases are tackled by the script.

Like Aisha M likes this
Aisha M
Contributor
September 30, 2020

hahaha @Antoine Berry  I get sooo happy when the scripts work ! TBH, yer the one composing the entire script (I do nothing) . .  but testing the change & seeing it work in Dev is a different kind of happiness :D:D

Actually this & the other listener issue is my final (almost) task before I could go on my leave, I have no clue how to fix that listener issue  :'( 

Aisha M
Contributor
October 14, 2020

Hi @Antoine Berry , I'm using your behavior (script below) & it behaves the below mentioned way (good way),

* Makes drop-down field mandatory when the current Due Date value is changed to a latter date

* Hides drop-down field when the current Due Date value is changed to a earlier date

* Stays hidden when an user randomly edits an Epic for other field values

But the only issue I faced is, sometimes an user creates a new Epic without a Due Date value (Maybe they wanna add a date after sometime) . And even in that case, when they try to fill Due Date for the first time, the Reason drop-down field shows as mandatory. Is it possible to avoid in that scenario alone ? (like, when an user already created an Epic & tries adding the Due Date for the first time ?)

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

import java.sql.Timestamp

@BaseScript FieldBehaviours fieldBehaviours

if (underlyingIssue){
def duedateField = getFieldById("duedate")
def duedate = duedateField.getValue()

int textFieldId = 10002
def textField = getFieldById("customfield_" + textFieldId)


def dueDate_value = underlyingIssue?.getDueDate()
if (duedate != "" && dueDate_value >= duedate){
textField.setHidden(true)
textField.setRequired(false)
}
else {
textField.setHidden(false)
textField.setRequired(true)
}
}

Can you please with this :( I have tried modifying but doesn't work :P

Antoine Berry
Community Champion
October 21, 2020

Hi @Aisha M ,

Please try this script : 

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

import java.sql.Timestamp

@BaseScript FieldBehaviours fieldBehaviours

if (underlyingIssue){
def duedateField = getFieldById("duedate")
def duedate = duedateField.getValue()

int textFieldId = 10002
def textField = getFieldById("customfield_" + textFieldId)

def dueDate_value = underlyingIssue?.getDueDate()
if ((duedate != "" && dueDate_value >= duedate) || !dueDate_value) {
textField.setHidden(true)
textField.setRequired(false)
}
else {
textField.setHidden(false)
textField.setRequired(true)
}
}

Note : I have not tried it so I cannot guarantee it will work, so let me know how it went.

Aisha M
Contributor
October 21, 2020

Hello @Antoine Berry , I think its working now. When I fill a value for the Due Date for the first time, it doesn't make the text field mandatory :):)

Just a random question, sometimes, the behaviour doesn't work at all & I m able to modify the Due Date from the issue card. Then when I refresh the issue, the behaviour works fine. Happens like maybe 4/10 times. Could it be due some issue in my DEV environment ? Because, this did not happen in a different environment. But I m unable to test that again there, coz that environment is down... lol

Like Antoine Berry likes this
Antoine Berry
Community Champion
October 21, 2020

Do you modify the due date directly on the view issue screen ? Because, behaviours on inline fields do not work if I remember correctly. 

Aisha M
Contributor
October 21, 2020

@Antoine Berry Nope ! Usually, what happens is when I try to modify the Due Date, the edit screen pops up & the behaviour works as expected making the text field mandatory/or hidden (Good behaviour).

But, on certain occasions, I m able to modify the due date directly from the view screen. And when I refresh the issue and try again, it works properly & the edit screen pops up as expected. I'm not sure if its due to some issue with my environment.

Like Antoine Berry likes this
Antoine Berry
Community Champion
October 21, 2020

Absolutely this is what is supposed to happen. I think there are some issues with the inline edit and behaviours such as : https://productsupport.adaptavist.com/browse/SRJIRA-3004

I am not sure about your particular issue though... 

Are you using Internet Explorer ?

Aisha M
Contributor
October 21, 2020

@Antoine Berry  I use chrome. But, maybe there is a teeny tinyy delay for the behaviour to kick in or something. I wish our second environment comes back up, so I can test again to see if its due to the behaviour as such or the environment itself ! O_o

Antoine Berry
Community Champion
October 22, 2020

Yeah let me know the out come if you do so :)

Aisha M
Contributor
October 23, 2020

@Antoine Berry Sure :):) Thank you so much for helping out with the million changes with the script ^_^

Like Antoine Berry likes this
Antoine Berry
Community Champion
October 23, 2020

As I said, I am happy to help, it is fun to me. Might as well enjoy it as long as Jira server (RIP) is around :)

Aisha M
Contributor
August 12, 2021

@Antoine Berry Hi !!!! Hope you are doing good !!!! :) Had to temporarily move out of JIRA and go on a super long break, hence hadn’t bothered you much with any new questions :D lol 

anyway, thank you soooo sooo much for having helped me with my 27378377292 questions and follow ups :p 

Like Antoine Berry likes this
Antoine Berry
Community Champion
August 30, 2021

Hi @Aisha M ,

I am doing super good, was on holiday for the last three weeks... it's a rough come back !

Hope you're doing good as well ! Have you abandoned Jira ? I have been quite busy lately so your questions were a good way to keep up with the community. It was fun to answer them :)

Like Aisha M likes this
Aisha M
Contributor
September 17, 2021

@Antoine Berry  I might be getting back to my old JIRA project in a month or two . .  Not sure though :D I hope I still get help from the community like I used to if I'm back to JIRA :D 

At the moment working on some other roles :) Just posted a question here after what seems like a million years :)

Antoine Berry
Community Champion
September 17, 2021

Hi @Aisha M , I am sure you will always find help on the community. Anyway feel free to @ me if you need help with a question, I actually need to get some kudos for accepted answers. :)

Aisha M
Contributor
September 17, 2021

@Antoine Berry It isn't related to JIRA tbh, but in case you are curious :)

0 votes
Aisha M
Contributor
August 19, 2020

@Antoine Berry  Can I please have your thoughts on this :) 

Aisha M
Contributor
February 7, 2024

@Antoine Berry  Hellooo ! Hope you are doing amazing ! I finally logged back into my older account, was just too lazy to reset the pwd . .  :D:D 
Lol, cannot believe how much I had bothered you with my questions ! Thank you so much for always responding to them & helping me out . .  I don't think I would have survived in my project without your help . .  But, I still suck at coding though . .  :D

Antoine Berry
Community Champion
February 7, 2024

Hello @Aisha M 

I am doing fantastic, glad to hear from you ! It is always a pleasure to be able to help the community. No doubt that you have made great progress throughtout the journey anyway. 

Have you moved to DataCenter ? Automation will help you without coding. :)

As a matter of fact, I actually need to answer questions so if you need any help do not hesitate to ping me !

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
TAGS
AUG Leaders

Atlassian Community Events