Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to set numeric range [1-100] for custom field?

RSP
Contributor
July 6, 2021

“Capacity” field should be an integer value between 1 and 100 inclusive. I’d like to validate that value and throw an error message if outside that range, typically validation done upon hitting “Create”.

1 answer

0 votes
Payne
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.
July 6, 2021

Do you have any apps, such as ScriptRunner? Without one of those, I don't know if you can accomplish this.

RSP
Contributor
July 6, 2021

Yes, I have ScriptRunner. 

Payne
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.
July 6, 2021

In that case, one way to accomplish your goal is to create a "Simple Scripted Validator" with a Condition like this:

def capacityField = customFieldManager.getCustomFieldObjectsByName("Capacity")[0]
def capacityValue = issue.getCustomFieldValue(capacityField)
return capacityValue == null || ((int)capacityValue >= 1 && (int)capacityValue <= 100)

And an error message like this:

Capacity should have a value between 1 and 100, inclusive.

Like Nic Brough -Adaptavist- likes this
RSP
Contributor
July 6, 2021

Is it possible to write Behavior for capacity field?

RSP
Contributor
July 6, 2021

I'm sorry but ScriptRunner is completely new for me...

I don't know how It'll show an error.

Will you please add complete code with an error.

RSP
Contributor
July 7, 2021

I have written this code in behavior for capacity field.

but It shows an error the variable issue is undeclared.

plz correct code ....

 

import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.component.ComponentAccessor


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfValues = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Capacity")
def cValue = issue.getCustomFieldValue(cfValues)

if ((int)cValue < 1 || (int)cValue > 100) {
throw new InvalidInputException("Capacity field should be an integer value between 1 and 100 ")
}

Payne
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.
July 7, 2021

For a "Simple Scripted Validator", there is a field where you type the error text that you wish to display on a failure.

RSP
Contributor
July 7, 2021

Thank You

Suggest an answer

Log in or Sign up to answer