Hi all,
I need to check if a date that is inserted from the user that creates a new issue is compliant to some criteria: such as 1. the date need to be minor or equal to today, 2. the date need to be greater than today minus 3 months.
I thought to use the Script Runner plugin with a behaviuor:
every time that this date change I do all checks and if its fail I reset the date and I modify the description specifying criteria, but my problem is that i don't understand how I can obtain a Data value from my customfield
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//Getting custom field value
def cf = getCustomFieldValue( customFieldManager.getCustomFieldObjectByName("Date of Loading") )
def today = new Date()
def cfValue = -- here I don't know how to pick the date value of my field
if (cfValue.after(today())
{.... }
Some one can help me?
Thank you!
Simona
Community moderators have prevented the ability to post new answers.
Hi Simona,
I am unsure exactly what your requirements are as the above does not fully make sense.
What I understand is that you are looking to ensure that the date entered in a date field is not before the current date or after the date in three months time.
If so this can be done using the Behaviours module of ScriptRunner by adding a behaviour onto the date field that you would like to control.
I have enclosed a sample script below which shows how this is implemented inside a behaviour.
// required import statements import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.user.util.UserUtil import java.util.Date.* // Get pointers to my date field and get its value as a date def cf = getFieldByName("Start Date") def cfval = cf.getValue() as Date // get todays date def today = new Date() // get yesterdays date def yesterday = today.minus(1) // get the date three months in the future def threeMonthDate = today.plus(90) // if the date entered is before todays throw an error if(cfval.before(yesterday)) { cf.setError("You must not enter a date that is before todays date") // if the date entered is over three months throw an error }else if(cfval.after(threeMonthDate)) { cf.setError("You must not enter a date that is over three months old") }else{ cf.clearError() }
I hope this helps
Kristian
Thank you very much Christian
My problem was that I missed a library in my script...import
java.util.Date.* and it didn't works...now it is all ok!
Thank you!
PS. criteria were these: (sorry for my english)
if
(cfval.after(today)) {
cf.setError(
"You must enter a date that is before todays date"
)
}
else
if
(cfval.before(threeMonthDate)) {
cf.setError(
"You must enter a date that is over three months old"
)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Simona,
I am glad that i was able to help you resolve your issue.
Thanks
Kristian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
you're better off using a validator, unless you want to do something like make the date window dependent on other fields. For a behaviour, see Kristian's example.
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.