I have two custom fields in JIRA
1: Completion due date
2: Issue Submitted date
I need the completion due date field to throw an error if the completion due date entered
is in the past date (before current date) and also it should not be before the Issue submitted date.
How do I get this using script runner?
Thank you
Hi @Garden16_
For your requirement, for the validation to work correctly, it would be best to create 2 Server-Side Behaviour configurations.
The first would be for the Completion due date field, and the second would be for the Issue Submitted date field.
You will need to add both of these Server-Side Behaviour configurations to ensure if there is a change in either the Issue Submitted Date field or the Completion Due Date, the value in the Completion Due Date field is correct based on your conditions and the Behaviour validation message is displayed or removed accordingly.
Below is the Server-Side Behaviour code for the Completion Due Date field:-
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def completionDueDate = getFieldById(fieldChanged)
completionDueDate.clearError()
def currentDate = new Date(System.currentTimeMillis())
def issueSubmittedDate = getFieldByName('Issue Submitted Date')
if (completionDueDate.value && issueSubmittedDate.value) {
def completionDueDateValue = completionDueDate.value as Date
def issueSubmittedDateValue = issueSubmittedDate.value as Date
if ( (completionDueDateValue < issueSubmittedDateValue) || (completionDueDateValue > currentDate) ) {
completionDueDate.setError('Incorrect Date')
}
}
Below is a screenshot of the Server-Side Behaviour configuration for the Completion Due Date field:-
Below is the Server-Side Behaviour code for the Issue Submitted Date field:-
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def completionDueDate = getFieldByName('Completion Due Date')
completionDueDate.clearError()
def currentDate = new Date(System.currentTimeMillis())
def issueSubmittedDate = getFieldById(fieldChanged)
if (completionDueDate.value && issueSubmittedDate.value) {
def completionDueDateValue = completionDueDate.value as Date
def issueSubmittedDateValue = issueSubmittedDate.value as Date
if ( (completionDueDateValue < issueSubmittedDateValue) || (completionDueDateValue > currentDate) ) {
completionDueDate.setError('Incorrect Date')
}
}
Below is a screenshot of the Server-Side Behaviour configuration for the Issue Submitted Date field:-
Please note that the sample codes provided are not 100% exact to your environment. Hence, you will need to make the required modifications.
Below are a few test screenshots:-
1. For the first test, the Issue Submitted Date value is set to 17 October 2022, and the Completion Due Date value is set to 27 October 2022.
There is no error in the validation because both conditions are met, i.e. the Completion Due Date value is after the Issue Submitted Date, and it is before or on the current date.
2. The Completion Due Date value is set before the Issue Submitted Date value for the second test. As expected, the Behaviour validation error message is returned.
3. For the third test, the Completion Due Date is set after the current date, i.e. 28 October 2022.
As expected, the Behaviour validation returns the error message.
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
This sounds like something for https://docs.adaptavist.com/sr4js/latest/features/behaviours/behaviours-tutorial
My idea was to:
- create a new behaviour
- map it with your project+issuetype(s)
- add "Completion due date" as a Field and add a server-side script to it
The last part is important because the code should run each time that you modify this field.
And then depending on the values user picks, it will run it's own thing and decide whether to set an error or not.
This is the snippet I was testing this with:
import com.onresolve.jira.groovy.user.FormField
FormField completionDueDateField = getFieldById(getFieldChanged())
Date enteredDate = (Date) completionDueDateField.getValue()
// used to see the values for debugging purposes
//getFieldById("description").setFormValue("Entered: " + enteredDate + ", Current: " + (new Date()).clearTime())
if (enteredDate != null) {
Date currentDate = new Date()
currentDate.clearTime() // clear the time portion of this
if (enteredDate < currentDate) {
completionDueDateField.setError("Date cannot be in the past!")
return
}
FormField issueSubmitedDateField = getFieldByName("Issue Submitted date")
Date issueSubmittedDate = (Date) issueSubmitedDateField.getValue()
if (issueSubmittedDate != null && enteredDate < issueSubmittedDate) {
completionDueDateField.setError("Date cannot be before Issue Submitted date!")
return
}
}
completionDueDateField.clearError() //unless we detected invalid date, we should clear the error
NOTE - this assumes "Date Picker" fields - without time. If you have a date time picker then this needs to be adjusted to also deal with the time portion of the value. Here, it's just a plain date to date comparison.
However, this doesn't work as it should and I suspect there is a bit of a bug going on in Scriptrunner.
You could try if this works for you, but I wonder if somebody from Adaptavist could confirm.
What I am observing is that the change of the date value does not get correctly picked up. It only registers when the entered date is earlier than today - and it will set the error. However, if I correct this date to today or tomorrow, it doesn't "register" the change and it does nothing. If I choose a different date in the past, then again it registers and sets the error.
If I set the "Issue Submitted date" to have a value, then it works correctly and picks up on the changes every time, as expected.
So something fishy is going on there, but unless I am brain damaged then the code is written correctly, so no idea why it works that way for me locally.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This works for the most part,I need an error in these scenarios
1: If the completion due date is prior to Issue submitted Date . Which I am getting .
If the issue submitted date is October 11 and the Completion Due date is October 3rd ,I am getting this error. _ Thank you for this.
2: But for second scenario , I would like to get an error if the Completion due date is prior to current date but after the Issue submitted Date
For example: If the issue submitted Date is Oct 3rd and if I enter Completion due date as October 24 th today (October 27 th) in the completion due date field , then I should get an error saying that "Completion date cannot be in the past"
looking for these two errors
1: "Completion Due Date cannot be prior to Issue submitted Date " Which I am getting .
2: "Completion due Date cannot be in the past" (In scenarios where the Completion due date is after Issue submitted date but prior to current date) Any dates entered after October 3rd and but before today.
3: The Completion due date (which is an estimated date) can be a future date. User Can select October 28 th and it should be fine.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Garden16_ ,
Have you gone through the solution I have provided in my comment? I have already provided a sample code for both those requirements.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.