Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner Validator - Require future date for custom date field

Michael
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 22, 2024

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

2 answers

1 accepted

0 votes
Answer accepted
Michael
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 25, 2024

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!")

}

 

0 votes
Sean Chua _Adaptavist_
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 25, 2024

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:

def requiredTime = startDate + 20
if (!endDate.after(requiredTime)) {
targetEndDate.setError("The 'Target End' date must be at least 20 days in the future from the 'Target Start' date.")

This is my create screen when it is less than 20 days:
image.png

And this is >20days:
image.png
Hope this helps.
Sean
Michael
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 25, 2024

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.

Suggest an answer

Log in or Sign up to answer