script runner validator- select list field is empty

Hello

I am trying to create script runner validator that should check select list custom field and another field based on select list field.

E.g. I have select list FieldA with values "Yes", "No" and "None" (empty). I need to check if FieldA is "Yes", then "FieldB" should be > 0. So my current working validator is

(cfValues['FieldA'].getValue() == "Yes" && cfValues['FieldB'] > 0) || (cfValues['FieldA'].getValue() == 'No')

But I need also to check if fieldA is "None", and I am unable to find correct statement for it I tried

(cfValues['FieldA'].getValue() == "Yes" && cfValues['FieldB'] > 0) || (cfValues['FieldA'].getValue() == 'No') || (cfValues['FieldA'] == null)

but it seems it is doesn;t work

What is the correct statement? Is it possible to optimize validator expression (without writing expression in one row) e.g.:

if(fieldA=="Yes"){
   if(fieldB<=0){
      //Validator was NOT passed
      // show message
   }
   else{
      //validator was passed
   }
}
else{
   //validator was passed
}

Thank you in advance,

Fyodor

4 answers

1 accepted

0 votes

It's better to use Groovy Truth than comparisons with null. So to check if it has a value:

if (cfValues['FieldA']) ...

to check if it doesn't have a value:

if (! cfValues['FieldA']) ...

You don't need to write it in one line, if you hit enter the text box will expand. Seems to be a common misconception that it must be a one-liner... bad UI design probably.

If it's clearer to write it in 10 lines with "if"s and "return"s then definitely do that.

Hey there,

"!= null" instead off ">0" could work and
you could check if you imported all nessesary data.
Like "import com.atlassian.jira.issue.Issue;"

That was often my problem ..

Hope this can help.

Greets
Simon

Hello Jamie and Simon.

Thank you for fast reply.

Could you describe me how can I put multi-row code? Expression should return TRUE (see screenshot), so I don't understand how can I put code with if, else etc ?

Could you show me an example?

Plugin version is 2.1.17

Best regards,

Fyodor

Like:

if (1) {
    return true
}
else {
    return false
}

am I missing someting?

The return keyword is actually optional for the last statement executed, but you can put it in for clarity.

Hello Jamie

Thank you for assistance, it works.

 

Fyodor

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,760 views 11 18
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot