I'm trying to write s groovy validator or create to check if custom field set to Y

Kelvin Fielding August 16, 2017

I have a Select list, Choices Y, N. If user selects Y I unhide a date field and alert them to fill it in.

Now I want a Validator on create issue that Checks if choice is Y, if it is then is date field set and greater than today

My code doesn't work though:-

import com.atlassian.jira.component.ComponentAccessor;
def customField =  ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_39464");
def value = (String)issue.getCustomFieldValue(customField); 
 
//and now simply check
if(value == "Y"){
 Date now = new Date()
 Date cfDate = new Date(cfValues['Submission to RMGC Date'].getTime())
 new Date(now.getYear(), now.getMonth(), now.getDate()).compareTo(cfDate) <= 0
}
else {
 result = false
}

What am I doing worng? 

1 answer

1 accepted

0 votes
Answer accepted
Joshua Yamdogo @ 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.
August 16, 2017

Hi Kelvin,

Your script is very close. I see a couple of issues in your script:

  • It looks like you forgot to define "result" in your script.
  • You should use "< 0" if you want dates greater than today. "<= 0" will include today.
  • Your else statement should just return true. If the value is not Y, there is nothing to validate. You should return true and let them create the issue since there will be no date field present.
import com.atlassian.jira.component.ComponentAccessor

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_39464")
def value = issue.getCustomFieldValue(customField) as String

//and now simply check
if(value == "Y") {
log.debug("checking date")
Date now = new Date()
Date cfDate = new Date(cfValues['Submission to RMGC Date'].getTime())
return new Date(now.getYear(), now.getMonth(), now.getDate()).compareTo(cfDate) < 0
}
else {
true
}

 After those changes, the validator works for me:

Screen Shot 2017-08-16 at 10.24.46 AM.png

Kelvin Fielding August 17, 2017

Thanks for the suggestions. I don't have access to the logs so cant check them.

I made the changes but it still doesn't work. No date, yesterday's date or today's date all work, an issue is created. Even setting false is the choice is not "Y" still works when I select "N".

Is there something I am not uderstanding about validators on Creae issue?

Joshua Yamdogo @ 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.
August 17, 2017

Hi Kelvin,

This is probably a dumb question, but did you remember to publish the workflow after adding this validator?

I'd also make sure you have the right custom field names. Go to the custom fields page and make sure that you have the correct ones. 

You might try using getCustomFieldObjectByName in the second line of the script and just put in the name of the custom field, instead of trying to use the ID.

import com.atlassian.jira.component.ComponentAccessor

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Your field name here")
def value = issue.getCustomFieldValue(customField) as String

//and now simply check
if(value == "Y") {
log.debug("checking date")
Date now = new Date()
Date cfDate = new Date(cfValues['Submission to RMGC Date'].getTime())
return new Date(now.getYear(), now.getMonth(), now.getDate()).compareTo(cfDate) < 0
}
else {
true
}
Kelvin Fielding August 17, 2017

Yes I did publish and I've tried using the field name but same result. I used the cfID as I have 3 other fields I will need to do the same check on.

I'm going to try on my own server tonight which is Jira 7. I'm using Jira 6.4.11 here at work. Also the plugin is v3.1.4....

Thx for your help though.

Joshua Yamdogo @ 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.
August 17, 2017

Ah, using ScriptRunner 3.1.4 and JIRA 6 may have something to do with it. I am testing on JIRA 7.4.1 and ScriptRunner 5.0.18. That could be why it works for me and it does not work for you. Perhaps you could convince your workplace to purchase the newest version of ScriptRunner? Kidding, of course... 😉

Let me know how testing on your own server goes. 

Kelvin Fielding August 18, 2017

YHes, tried on my own server running 7.4.x an d the laatest Scriptrunner. It all works.

 

I've been told process is lower priority now and reporting is important. Like mi;eston e due, missed, etc. Not much point IMHO if they don't complete the date fields... Ho Hum.

 

Thx again for your help.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events