Forums

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

Disallow past date in date custom field

Anand Dandikar March 18, 2021

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

1 answer

0 votes
Trudy Claspill
Community Champion
March 18, 2021

Hello @Anand Dandikar 

Please show us what you have tried that has not worked.

Anand Dandikar March 19, 2021

@Trudy 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.Category
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.pico.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.opensymphony.workflow.InvalidInputException

// replace 10000 by number of date time custom field that must be validated
def 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.NullPointerException
java.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.

Thanks,

Anand

Trudy Claspill
Community Champion
April 13, 2021

Have you tried renaming your variable, as specified in the warning?

"Please choose a different name for variable "componentManager": @ Line 13, Column 18."

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.8.0
TAGS
AUG Leaders

Atlassian Community Events