Thank You in advance! I'm trying to validate the regular expression of a single line custom text field (Incident/s), but only when it becomes required.
Background:
During the Defect create validator, I have a ScriptRunner Behavior on the custom field (Incident/s) to make it required only if the custom field (Found Through) has a value that equals (Production). Then only the custom field (Incident/s) becomes required.
Script:
def field1 = getFieldByName("Incident/s")
def field2 = getFieldByName("Found Through")
def field = getFieldByName("Incident/s")
def val = field.getValue() as String
if(field2.getValue().equals("Production"))
{ field1.setRequired(true) }
else { field1.setRequired(false) }
Goal:
Validate the regex of the single line text field (Incident/s) once it becomes required, to ensure that it begins with "INC" or "RITM" followed by 7 digits.
Help:
def field = getFieldById(getFieldChanged())
def val = field.getValue() as String
if (!val.matches(/(?i)(INC|RITM)[0-9]{7}/)) {
field.setError("Required Format! Must be [INC#######] or [RITM#######]")
} else {
field.clearError()
}
Issue:
I placed this into a second field server-side script within the Behavior. It is working, but how do I handle the blank field value? It is failing the regex validator since its blank. I need it to only regex validate once the field becomes required.