Custom date field to auto-populate with current date?

Laci May 24, 2021

I have a custom field for our review board that is a single select drop down menu with the options of "yes" or "no". If "no" is selected, nothing happens. However, if "yes" is chosen, we have a Reviewed Date field that appears with using behaviors on scriptrunner (we need this because you can't search on when custom fields were changed).

For that date selection field, I'd like to auto-populate with the current date when "yes" is selected. Any ideas on how to achieve this? 

Thanks in advance!

1 answer

1 accepted

1 vote
Answer accepted
Max Lim _Adaptavist_
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.
May 24, 2021

Assuming Reviewed Date is a Date Picker field, you can attach following Behaviour snippet to the field:

if (getFieldByName("yourSelectList").getValue() == "yes") {
getFieldById(getFieldChanged()).setFormValue(Calendar.getInstance().format("d/MMM/yy"))
} else {
getFieldById(getFieldChanged()).setFormValue(null)
}

This will set the current date on the field when option "yes" is selected and clear the value when option "no" is selected.

Laci May 24, 2021

Unfortunately, this does not work. When I select "yes" in our drop down, it will not allow me to add a date and it doesn't add a date on its own either.

Max Lim _Adaptavist_
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.
May 24, 2021

1. Did you change "yourSelectList" to the name of your select list?
2. Is your option value name "yes" or "Yes"?
3. What do you mean by it will not allow you to add a date?
4. Is Reviewed Date field a Date Picker field?

Can you post your original Behaviour script in text on how you make Reviewed Date field appears as well as the screenshots of the behaviour configurations?

Above snippet is for you to incorporate into the original Behaviour script.

Max Lim _Adaptavist_
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.
May 24, 2021

The important part is how to set current date on a Date Picker field:

getFieldById(getFieldChanged()).setFormValue(Calendar.getInstance().format("d/MMM/yy"))
Laci May 24, 2021

1. Yes, I changed the "yourSelectList" to the name of my field.

2/3. I updated the selection to match my list, it is "Yes", not "yes" so that was why it wouldn't allow me to add a date I'm assuming. So that answers question 3.

4. Yes, Review Date is a Date Picker field.

 

It's still not populating a date even though I've made updates to the code. I unfortunately cannot post screenshots as my environment is not unclassified. 

 

def ReviewedDate = getFieldByName("Reviewed Date")

def Reviewed = getFieldById(getFieldChanged())

def selectedOption = Reviewed.getValue() as String

def isYesSelected = selectedOption == "Yes"

ReviewedDate.setHidden(!isYesSelected)

def selectlistselection = getFieldByName("Reviewed").getValue()

def reviewedDate = getFieldByName("Reviewed Date")

if (selectlistselection == "Yes") {

reviewedDate.setRequired(true)

}

else {reviewedDate.setRequired(false) 

}

Max Lim _Adaptavist_
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.
May 24, 2021

Hi @Laci 

This should work (I also make the script more consistent and clearer):

def ReviewedDate = getFieldByName("Reviewed Date")
def Reviewed = getFieldById(getFieldChanged())
if (Reviewed.getValue() == "Yes") {
    ReviewedDate.setHidden(false)
ReviewedDate.setRequired(true)
    ReviewedDate.setFormValue(Calendar.getInstance().format("d/MMM/yy"))
} else {
    ReviewedDate.setHidden(true)
    ReviewedDate.setRequired(false)
    ReviewedDate.setFormValue(null)
}

Please note that if "No" is selected the date is cleared. If this is not your intention you can delete "ReviewedDate.setFormValue(null)".

Hope this helps!

Max

Like Lakshmi Ummalaneni likes this
Laci May 24, 2021

@Max Lim _Adaptavist_ Thanks so much! This appears to be working! I appreciate your help!

Kevin Comco June 8, 2021

@Laci I'm trying to do something very similar.  I've been trying to do it in a Listener, but perhaps the Behavior is appropriate too.  Would you mind posting your code here?

Laci June 8, 2021

@Kevin Comco Try the script that @Max Lim _Adaptavist_ wrote above. That's what I'm using. You'll just need to update the field names.

Kevin Comco June 8, 2021

@Laci thanks for the reply.  I'm having trouble getting the right classes for a couple of the methods (e.g. setFormValue) and was hoping to see your imports, full context, etc...

Quick question, are you able to JQL query on the dates you end up setting?  I'm able to get my date picker to visually look like it's all good, but JQL fails to find anything because it thinks the field is EMPTY.

Suggest an answer

Log in or Sign up to answer