It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Kelvin Fielding Aug 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] Community Leader Aug 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 Aug 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] Community Leader Aug 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 Aug 17, 2017 • edited

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] Community Leader Aug 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 Aug 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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in Marketplace Apps & Integrations

Demo Den Ep. 4: Continuous Integration with CircleCI & Jira Software

Get ready! Demo Den Episode 4 is coming your way on Tuesday, May 28, 2018 with a Continuous Integration and Delivery special demo. CircleCI Director of Solutions Engineering, Eddie Webb will show us ...

530 views 0 4
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you