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!
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The important part is how to set current date on a Date Picker field:
getFieldById(getFieldChanged()).setFormValue(Calendar.getInstance().format("d/MMM/yy"))
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.
Register NowOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.