check start time inserted in create screen compliant to my criteria

subham patra
Contributor
February 2, 2021

Hello 

Can you please help edit the code below? 

Goal: I need to validate a datetime picker that user should be able to pick past date based on other field values. for ex. user should able to pick past date only if "optionA" is selected on Customfield1. 

I am trying using scriptrunner behavior. Any answer or guidance would be highly appreciated. :-) 

- "Change Request Type" field has Latent, Emergency and Normal values.

- Start-Time is a datetime picker.

// required import statements
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
import java.util.Date.*

// Get pointers to my date field and get its value as a date
def chngRqstType = getFieldByName("Change Request Type");
def starttime = getFieldByName("Start-Time")
def stvalue = starttime.getValue() as Datetime

// get todays date
def today = new Date()

if (chngRqstType.getValue() == "Emergency" || chngRqstType.getValue() == "Normal" ){
stvalue >= today
}
else if {
starttime.setError(" If you would like to log ticket that release has been implemented, please select change request type as Latent")
}

 

1 answer

0 votes
Helmy Ibrahim _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.
February 6, 2021

Hi @subham patra

You can use the method .after() from java.util.Date to do the comparison. 

Here I have amended your code:

def chngRqstType = getFieldByName("Change Request Type")
def chngRqstTypeValue = chngRqstType.getValue()

def starttime = getFieldByName("Start-Time")
def stvalue = starttime.getValue() //This returned Date by default

def today = new Date()

def checkWith = ['Emergency', 'Normal']

if (stvalue) {
if (chngRqstTypeValue in checkWith && stvalue.after(today)) {
starttime.setError("If you would like to log ticket that release has been implemented, please select change request type as Latent")
} else {
starttime.clearError()
}
}

You can safely ignore the Static Type Checking on "stvalue.after(today)". Groovy doesn't know what's the object type of stvalue on the script editor. Your code will run just fine.

Also, you can use the same script in both Change Request Type field and Start-Time field in behaviour or else you would need to interact again with this field after you made a change on another field to re-trigger the behaviour.

I hope this helps.

Suggest an answer

Log in or Sign up to answer