Text field scripted workflow validator

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
Vasiliy Zverev Community Champion Dec 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.

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 Community Champion Dec 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).

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 Community Champion Dec 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");

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 Community Champion Dec 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?

1 vote

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.

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published 8 hours ago in Off-topic

Get to know our Atlassian User Group Leaders from Bengaluru, India

Meet @Dinesh Dhinakaran, @Vishnu Vasudeva, @Rajeev Verma, and Jamshid Nalakath: Our extraordinary AUG leaders from Bengaluru, India. These four work together to strengthen the bonds of their local co...

82 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you