Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Check if a date inserted in the create screen is compliant to my criteria

Simona Persico April 8, 2016

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

 

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 8, 2016

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

 

Simona Persico April 10, 2016

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: smilesmile (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")
}

 

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 11, 2016

Hi Simona,

I am glad that i was able to help you resolve your issue.

Thanks

Kristian

0 votes
JamieA
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.
April 10, 2016

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.

TAGS
AUG Leaders

Atlassian Community Events