Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×There must be an answer already for this, but I've not been able to find it.
We are using JIRA Server 7.6.4 and we have Adaptavist ScriptRunner v5.3.9.
I am a developer, but I'm new to script writing in JIRA, and with Groovy.
(Side note - if you have recommendations for tutorials on this topic, I would welcome the pointers.)
I want to validate that the value entered in a custom Number Field is within a range.
I've reviewed the ScriptRunner documentation here: https://scriptrunner.adaptavist.com/latest/jira/quickstart.html
I've tried creating a Behavior, and a workflow Validator, but none of the scripts I have tried have been correct.
The field name is "Planned Spend Percentage". In my test issue I currently have it set to -1.
I've tried using a Simple Scripted Condition to validate my code.
{code}
cfValues['Planned Spend Percentage'] < 1
{code}
For the above the Simple Scripted Condition says "The result evaluated to true".
But if I put the same code into a Custom Script Validator, I get an error reported in the Validator.
My code
{code}
import com.opensymphony.workflow.InvalidInputException
if (cfValues['Planned Spend Percentage'] < 1 || cfValues['Planned Spend Percentage'] > 100) {
throw new InvalidInputException("Resolution must not be fixed if not specifying a fix-version")
}
{code}
And the error reported is:
2018-04-12 21:28:09,755 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: BUDGET-1038, actionId: 51, file: <inline script> groovy.lang.MissingPropertyException: No such property: cfValues for class: Script339 at Script339.run(Script339.groovy:3)
I've also tried
{code}
getFieldByName("Planned Spend Percentage") < 1
{code}
In the Simple Scripted Condition the Preview function says:
"No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getFieldByName() is applicable for argument types: (java.lang.String) values: [Planned Spend Percentage]"
When I try the code in a Custom Script Validator I get:
2018-04-12 21:33:38,909 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: BUDGET-1038, actionId: 51, file: <inline script> groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getFieldByName() is applicable for argument types: (java.lang.String) values: [Planned Spend Percentage] at Script353.run(Script353.groovy:3)
Hi Trudy,
Here's how you do it:
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.component.ComponentAccessor
def cfValues = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Planned Spend Percentage"))
if (cfValues < 1 || cfValues > 100) {
throw new InvalidInputException("Resolution must not be fixed if not specifying a fix-version")
}
Hi Ivan,
Thank you so much! That works perfectly in the Workflow Validator.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.