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.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How do I validate the value of a custom Number Field?

Trudy Claspill
Contributor
April 12, 2018

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)

 

1 answer

1 accepted

0 votes
Answer accepted
Ivan Tovbin
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.
April 12, 2018

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")
}
Trudy Claspill
Contributor
April 12, 2018

Hi Ivan,

Thank you so much! That works perfectly in the Workflow Validator.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events