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 has the value of "A")
Here is my code so far:
import com.onresolve.jira.groovy.user.FieldBehavioursimport groovy.transform.BaseScriptimport java.util.Date.*@BaseScript FieldBehaviours fieldBehavioursdef fieldB = getFieldById("customfield_17800")def fieldBValue = fieldB.getValue()def DateFieldA = getFieldById("Customfield_11900")def DateFieldAValue = DateFieldA.getValue() as Date// Today's datedef today = new Date()// 20 Days in the futuredef requiredTime = today.plus(20)// if the date entered is before todays throw an errorif(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.LocalDateimport com.opensymphony.workflow.InvalidInputExceptiondef 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:
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.
It looks like you're new here. Sign in or register to get started.