Is there a way to build workflow validations on text fields?

Molly Favret March 21, 2012

I have several custom fields of type "Text Field (<255 characters)" and am wondering if there is a way to perform some workflow validations on the fields. For example, I have a "Zip Code" field that I know should always be 5 digits long. Any zip codes entered that are not 5 digits should be caught as invalid. Is there a way to build in these types of checks into the workflow as validators, or is there a way to further customize a text field by saying how many characters it should be ? I've searched Atlassian answers and the Plugin Exchange but haven't been able to find anything. Any help is appreciated!

Thanks!

2 answers

1 accepted

3 votes
Answer accepted
JamieA
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.
March 21, 2012

It's a very specific request so I doubt there is anything built-in. It's easy to do though with minimal programming... eg install script runner, go to validators, add validator, -> script validators -> simple scripted validator.

Then the condition in your case would be:

cfValues["ZipCode"]?.length() == 5

Obviously, double-check the name of your custom field.

JamieA
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.
March 21, 2012

Also don't forget to mark my answer as correct if it works for you ;-)

And you can mark my comments as helpful if you like.

Molly Favret March 21, 2012

Thanks! This works perfectly. Is it possible to use an OR statement? For example, my Zip field can be a 5 character string or N/A

cfValues['Current Zip'].length() == 5 OR cfValues['Current Zip'] == 'N/A'
 
This is what I tried but it is not working correctly. Thanks!
JamieA
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.
March 21, 2012

Thanks ;-) It all helps in my quest to get to 2nd place...

JamieA
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.
March 21, 2012

Just replace OR with ||

There is a condition tester builtin admin script, which is useful for honing your conditions/validators.

You can also do pattern matching, and more complex stuff than checking the length.

Molly Favret March 21, 2012

Thanks! Will the custom field validator work on Cascading select fields?

For example, in my cascade select field called "Request type", the user first selects "Retail" then selects "Add". This shows up as a concatenation in the "Request type" field as "Retail - Add". Will this be recongnized by the custom field validator?

Dieter
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.
March 21, 2012

+1 Jamie. Didn't know until know that custom field values are available as a map property :-)

JamieA
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.
March 21, 2012

Not in that way... for that example it would be like

def vals = cfValues["Request type"]?.allValues
vals[0] == "Retail" && vals[1] == "Add"

You can put as many lines in the condition as you want, it will expand as necessary.

Gory details: https://studio.plugins.atlassian.com/browse/GRV-42?focusedCommentId=45504&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-45504

Molly Favret March 21, 2012

Sorry for the multiple dumb questions, but I'm not an experienced groovy script user. Do I need to type more than the couple of lines mentioned above in my condition? It isn't working correctly and I am a bit lost since the link you are mentioning has a lot more too it than what I'm trying to do (I think).

def vals = cfValues["Request type"]?.allValues
vals[0] == "Retail" && vals[1] == "Add"

JamieA
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.
March 21, 2012

@Dieter - yeah, they're all chucked into the map for convenience. Only for conditions though IIRC. (and only in this plugin of course).

@Molly - > has a lot more too it than what I'm trying to do (I think)

what is it you're trying to do? I thought you wanted to validate a cascading select, but I'm sure there's more to it than that, as there's no point in validating that a field is only one value (in isolation).

Molly Favret March 21, 2012

I am trying to validate a cascading select field at a workflow step. I only want the workflow step to be available if the cascading select field meets a certain criteria. I can write simple conditionals using the built in scripts, I just don't know how to use Groovy and don't know where to start.

JamieA
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.
March 22, 2012

OK, so you need the Simple Script Condition (not validator). The condition for checking a cascading select should be what I put above... see what happens in the condition tester. Try that script both with an issue with the correct value, and issue with the wrong value, and an issue with no value. It might be necessary to check for nulls.

That will give information in the gui if the code fails to compile or whatever.

Molly Favret March 22, 2012

Sorry, I didn't mean to say validator. I am using a simple script condition as you mentioned. The condition tester is showing "false" for both an issue with the correct value and an issue with the incorrect value. When I try a null, I get this error:

javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getAt() on null object

I was thinking about adding a validator to ensure that the field cannot be null, but regardless both true and false cases are showing up as false.

This is my condition:

def vals = cfValues["Request Type"]?.allValues

vals[0] == "Retail" && vals[1] == "Add prescriber"

JamieA
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.
March 22, 2012

Things get complicated around cascading selects. Here is the condition:

cfValues.get("Request Type")?.getAllValues()*.value == ['Retail', 'Add prescriber']

Molly Favret March 22, 2012

Thank you so much! Works perfectly! I appreciate your quick responses and troubleshooting!

JamieA
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.
March 22, 2012

You're welcome...

Zhengyu Yong October 31, 2012

Hi Jamie,

I tried copying the format for the cascading select condition. But It doesnt seem to work for me.

I also get this error in the server logs

[groovy.canned.utils.ConditionUtils] javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: java.util.HashMap.getAllValues() is applicable for argument types: () values: []

What does this mean?

Molly Favret November 13, 2012

Hi Jamie,

We recently upgraded to Jira 5.1.6, and the Cascade Select Condition you suggested is no longer working. We cannot figure out what the problem is. Can you please advise?

Here is the condition:

cfValues.get("Request Type")?.getAllValues()*.value == ['Retail', 'Add prescriber'] || cfValues.get("Request Type")?.getAllValues()*.value == ['Retail', 'Add new address']

I am getting this error when running the Canned Script Runner:

javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: java.util.HashMap.getAllValues() is applicable for argument types: () values: []

JamieA
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.
November 13, 2012

Can you add this to the conditon:

log.warn "Request type: " + cfValues.get("Request Type")?
true

See what it says in the logs and report back pls...

Molly Favret November 13, 2012

I am working with someone who has access to the logs in this, and he added this and got the following error:

Script63.groovy: 29: expecting '}', found '' @ line 29, column 1.
1 error
2012-11-14 07:37:40,705 http-8080-5 ERROR tom.towkach 457x3287x1 atugrp 10.1.8.61 /browse/NNIICADMIN-38 [groovy.canned.utils.ConditionUtils] org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script64.groovy: 1: expecting EOF, found 'log' @ line 1, column 94.
', 'Remove existing address'] log.warn "
^
1 error
JamieA
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.
November 13, 2012

Can you post here the full condition. The lines I pasted above should be on new lines.

Molly Favret November 13, 2012

Jamie, thanks for your help. We actually ended up finding the fix. The syntax was changed to:

cfValues.get("Request Type")?.values()*.value == ['Retail', 'Add prescriber'] || cfValues.get("Request Type")?.values()*.value == ['Retail', 'Add new address']
 
Thanks for your help.
G June 7, 2016

Can we check number of words instead of length of string? 

0 votes
Michael Henry October 7, 2013

Hello all!

I was wondering if anyone knows of a way to do this with OnDemand?

Suggest an answer

Log in or Sign up to answer