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

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

This widget could not be displayed.

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

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?

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
}

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.

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. 

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published Jul 25, 2018 in Marketplace Apps

Jira Cloud and Bitbucket Cloud Integration with Microsoft Teams

One of the newest products in the Microsoft family - Microsoft Teams,  is a chat-based hub for teamwork that integrates all the people, content, and tools your team needs to be more engaged and ...

733 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you