Hi all,
I'm trying to create a Scriptrunner validator so that a custom date field (Field A) will not allow a user to select a date closer than 20 days from today, if a single select customfield (Field B) has the value of "A")
Here is my code so far:
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import java.util.Date.*
@BaseScript FieldBehaviours fieldBehaviours
def fieldB = getFieldById("customfield_17800")
def fieldBValue = fieldB.getValue()
def DateFieldA = getFieldById("Customfield_11900")
def DateFieldAValue = DateFieldA.getValue() as Date
// Today's date
def today = new Date()
// 20 Days in the future
def requiredTime = today.plus(20)
// if the date entered is before todays throw an error
if(fieldBValue = "A") {
DateFieldAValue.after(requiredTime)
}
Can someone help tell me where I'm going wrong?
Thanks,
Mike
After looking at the code above and researching a bit more - our team came up with new code which works for us.
If anyone else has this issue please see below for our working code:
import java.time.LocalDate
import com.opensymphony.workflow.InvalidInputException
def FieldA = issue.getCustomFieldValue(17800).toString()
def DateFieldValue = issue.getCustomFieldValue(11900).toString()
def Date = LocalDate.parse(DateFieldValue, "yyyy-MM-dd HH:mm:ss.A")
if (FieldAValue == "Value_Here" && (Date.isBefore(LocalDate.now().plusDays(20)))) {
throw new InvalidInputException("Your Error Message Here!")
}
Hey @Michael ,
You can use Behaviours for that to validate on the screen. I have a similar script which is to ensure Target End must be a future date from Target Start Date - ScriptRunner Jira DC - Behaviours (Date Picker Field B > Date Picker Field A) .
You probably need to change the validation portion:
if (startDate && endDate && endDate.before(startDate)) {
to something like:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sean,
Thanks for the hints - as we needed more than just the comparison of dates. I'll leave the completed code before for others who might have this same issue.
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.