Hello,
I am desperately trying to compare 2 dates from custom fields in the issue screen, but nothing happens and I can't find the problem in my code:
--------------------------------------------------------------------
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import groovy.time.TimeCategory
import java.util.Date
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import java.text.SimpleDateFormat
def cf1 = getFieldById("date 1") // EDIT: this was my mistake, getFieldByName should be used instead
def cf2 = getFieldById("date 2") // EDIT: SAME
Date DateVal1 = (cf1.value as Date)
Date DateVal2 = (cf2.value as Date)
if (DateVal1.getTime() < DateVal2.getTime()) {
cf2.setError("date 2 cannot be later than date 1")
}else{
cf2.clearError()
}
--------------------------------------------------------------------
Your help will be much appreciated!
I think you should use getFieldByName() iso getFieldById()
See the behaviors doc!
Oh of course you are right, rookie mistake... I was paying so much attention to the second part that I didn't see this one, which comes from a copy paste of another script on another field which is -obvisouly- not used (hin hin why do I keep it then ...)
Thanks for checking and replying!
Will try to fix all of this asap :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Why I failed to use the above method,the code is as follows,nothing happens and I can't find the problem in my code:
def StartDateField = getFieldByName("Start date")
def EndDateField = getFieldByName("End date")
Date StartDateValue = (StartDateField.value as Date)
Date EndDateValue = (EndDateField.value as Date)
if ( StartDateValue.getTime() > EndDateValue.getTime() ) {
EndDateField.setError("EndDateValue cannot be later than date StartDateValue")
} else {
EndDateField.clearError()
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How did you configure your Behaviour ?
What dates did you enter to test ?
In your code you test Start Date is later than End Date, and if so your give error "End Date cannot be later than Start date" ???
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
"Start Date" and "End Date" are "Date Pickers" type
What I want to achieve is:When I fill in the "End Date", if the "End Date" is less than the "Start Date", I will be prompted with an error
The above error message is wrong, please forgive me,I want to say:EndDateValue cannot be earlier thanStartDateValue
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Many things can be wrong, difficult to find the exact cause with only info "...nothing happens...".
Are the names of the field correct ?
Is the behavior correctly configured ?
I think best is to
- validate if there are messages in the log files
- add some debug entries to validate if the code is executerd (!), and what is in the variables StartDateValue and EndDateValue (not sure you construction is correct)
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.