It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Compare custom field dates in behaviour ? Edited

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

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

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

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

"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

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
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you