Hi All,
We have two date fields, i.e. "From" and "To". We want to implement following business logic on both fields:
1) Dates shouldn't be allowed in the past, better if we only allow date/time selection from now and any date/time in future
2) "To" date cannot be less than "From" date.
I have simulated an "Edit" transition that gives a workflow screen and I intend to use a workflow validator. We have Scriptrunner licensed. I referred to following pages, however I couldn't get a simple/custom script validator to work:
https://community.atlassian.com/t5/Jira-questions/Prevent-users-from-entering-a-date-in-the-past/qaq-p/213766
https://community.atlassian.com/t5/Jira-questions/Date-Custom-Field-has-to-be-show-upcoming-days-not-previous/qaq-p/1270264
Anyone worked on similar solution and has a script that would work for the criteria above?
Thanks,
Anand
Hello @Anand Dandikar
Please show us what you have tried that has not worked.
@Trudy P Claspill thank you for looking into the issue.
After the field has been associated with the context and screen, past date are still being allowed on the "From" date/time picker field.
My script is as follows:
import org.apache.log4j.Categoryimport com.atlassian.jira.issue.CustomFieldManagerimport com.atlassian.jira.component.pico.ComponentManagerimport com.atlassian.jira.issue.fields.CustomFieldimport com.atlassian.jira.issue.ModifiedValueimport com.atlassian.jira.issue.util.DefaultIssueChangeHolderimport com.atlassian.jira.issue.util.IssueChangeHolderimport com.opensymphony.workflow.InvalidInputException// replace 10000 by number of date time custom field that must be validateddef fieldname = "customfield_16200"ComponentManager componentManager = ComponentManager.getInstance()CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()CustomField field = customFieldManager.getCustomFieldObject(fieldname)Date value = issue.getCustomFieldValue(field)Calendar now = Calendar.getInstance()Calendar entered_date = Calendar.getInstance()entered_date.setTime value
if (entered_date.get(Calendar.MONTH) == now.get(Calendar.MONTH) && entered_date.get(Calendar.YEAR) == now.get(Calendar.YEAR))return;if (now.before(entered_date))return;def invalidInputException = new InvalidInputException("Entered date is invalid! From date must be set in future")
It throws a warning with a yellow triangle as follows:
Variable "componentManager" masks a binding variable of the same name. Please choose a different name for variable "componentManager": @ Line 13, Column 18.
Application logs shows:
2021-03-18 09:44:54,066-0400 http-nio-8080-exec-17 ERROR Anand.Dandikar 584x21519x1 9hhrid 10.3.205.167,0:0:0:0:0:0:0:1 /secure/AjaxIssueEditAction!default.jspa [c.atlassian.velocity.DefaultVelocityManager] MethodInvocationException occurred getting message body from Velocity: java.lang.NullPointerExceptionjava.lang.NullPointerException
When previewing the script with a test issuekey, I get:
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.component.pico.ComponentManager.getCustomFieldManager() is applicable for argument types: () values: [] at Script31.run(Script31.groovy:14)
JSU Automation Suite has data expression compare, however since we have already licensed Scriptrunner, we would prefer to implement this via a script validator. Please suggest.
Have you tried renaming your variable, as specified in the warning?
"Please choose a different name for variable "componentManager": @ Line 13, Column 18."
It looks like you're new here. Sign in or register to get started.