Text field scripted workflow validator

Paulina Wegrzyn December 14, 2015

Hi 

I have a problem with scripted validator - we do have some text custom field which has some default value assigned during creation of the issue (template) and when closing issue I want to check if default value (template) was modified.

Here is the validator which I use - it is working perfectly good when default value (template) is not complex (for example one sentence without enters)

if(!(issue.projectObject.key in ["TEST"])){
return true;
} else {
    if((cfValues['Condition1']?.value in ['Done', 'N/A']) && (!(cfValues['Comment to C1'] in [null, '_Template_']))){
		return true;
    } else {
return false;
    }}

It is checking if project is TEST, then if Condition1 is set to "Done" or "N/A" and then it should check if  field "Comment to C1" is not empty and is different than Template.

This is my Template which is used as default value of our text custom field:

_Please adapt this description field according to your needs._
_Examples:_ * first comment
* second comment

 

Do you have any hints how can I check if this field is not equal to this complex template? 

Thanks in advance,

Paulina

 

 

3 answers

1 vote
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.
December 15, 2015

Looks like a problem with spacing or newlines.

In your last example Paula you have a space at the end of the lines, but you may well not have that in your default field.

For a test try:

assert template == cfValues['Comment to C1']

It will blow up in a nasty way but the logged message should show you where they differ.

1 vote
Vasiliy Zverev
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.
December 14, 2015

Try to store this template like that:

String template = "_Please adapt this description field according to your needs._
_Examples:_                                                                                         * first comment   
         * second comment"

And then compare with this variable.

Paulina Wegrzyn December 15, 2015

Hi @Vasiliy Zverev,

Thanks for the reply.

I tried this -> I even used + concatenation operator at the end of each line for having a multi-line string (see code below)

String template;
template = String.format("_Please adapt this description field according to your needs._" +
"_Examples:_" +
"* first comment" +
"* second comment");

if(!(issue.projectObject.key in ["TEST"])){
return true;
} else {
    if((cfValues['Condition1']?.value in ['Done', 'N/A']) && (!(cfValues['Comment to C1'] in [null, template]))){
		return true;
       } else {
return false;
    }}

 

Do you have any idea why it does not work? (I am using simple scripted validator)

 

Thanks,

Paulina

Vasiliy Zverev
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.
December 15, 2015

You miss an "\n" to define new string (see this http://stackoverflow.com/questions/19008970/java-what-does-n-mean). Also pay a lot of attention for spaces. And more: to make validator work you need to create invalidInputException = new InvalidInputException("message") (see https://jamieechlin.atlassian.net/wiki/display/GRV/Validators).

Paulina Wegrzyn December 15, 2015

Adding "\n" didint helped and as I am using the simple scripted validator - there is build in message error 

111111.JPG

 

String template;
template = String.format("_Please adapt this description field according to your needs._ \n"+
"_Examples:_ \n"+
"* All stories done \n"+
"* No critical bug");
 
if(!(issue.projectObject.key in ["TEST"])){
return true;
} else {
    if((cfValues['Condition1']?.value in ['Done', 'N/A']) && (!(cfValues['Comment to C1'] in [null, template]))){
        return true;
       } else {
return false;
    }}

 right now valitator display error when "Comment to C1" is empty (of course if project is TEST and Condition1 is set to "Done" or "N/A") otherwise it accepts everything (also the template and it shouldn't)...

 

And if template has only one sentence - for example:

String template;
template = String.format("_Please adapt this description field according to your needs._ ");

 validator works correctly

 

 

Paulina

Vasiliy Zverev
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.
December 15, 2015

Let's define the trouble: how to check that value into a custom field is the same as for template. Is it so? As I can see you add \n and a space, namely " \n", but not "\n". Try this one: ("_Please adapt this description field according to your needs._\n"+ "_Examples:_\n"+ "* All stories done\n"+ "* No critical bug");

Paulina Wegrzyn December 15, 2015

yes, you defined my trouble perfectly:) I copied/pasted your proposition and still it does not work (only when "Comment to C1" is empty - error message is displayed). Spaces which I had were in the template before (I removed them from template in custom field and also pasted your proposition in scripted validator). Maybe there is something else missing?

Vasiliy Zverev
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.
December 15, 2015

Is it correct to say, that it will be enough to find substring "_Please adapt this description field according to your needs._"? Or we need to find exact match?

0 votes
Paulina Wegrzyn January 19, 2016

Hi, 

Sorry for that late answer. I managed to resolved it using such code (I decided to get rid of all new lines in code using replaceAll() when comparing template with field value and it started to work):

String template = '_Please adapt this description field according to your needs._'+"\n"+
'_Examples:_'+"\n"+
'* All stories done'+"\n"+
'* No critical bug';

if(!(issue.projectObject.key in ["TEST"])){
return true;
} else { 
if((cfValues['Condition1']?.value in ['Done', 'N/A']) && (!(cfValues['Comment to C1'] in [null])) && (!(cfValues['Comment to C1'].replaceAll("\n|\r", "") == template.replaceAll("\n|\r", "")))){
return true;
} else {
return false;
}}

 

Thanks for your support!

Greetings,

Paulina

Suggest an answer

Log in or Sign up to answer