Compare custom field dates in behaviour ?

Clement March 14, 2019

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!

 

 

2 answers

1 accepted

0 votes
Answer accepted
Marc Minten _EVS_
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.
March 14, 2019

I think you should use getFieldByName() iso getFieldById()

See the behaviors doc!

Clement March 16, 2019

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 :)

0 votes
zhangqian04 December 19, 2019

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()
}

Marc Minten _EVS_
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.
December 19, 2019

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" ???

zhangqian04 December 19, 2019

"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

Marc Minten _EVS_
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.
January 2, 2020

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)

Suggest an answer

Log in or Sign up to answer