How to Create a Simple Scripted Validator?

Good day.  

I have a custom field A (multi-select checkboxes) and a custom field B (numeric), and need help in crafting the syntax for the following simple scripted validator:

When the "A" field equals "LIFE", the "B" field must be populated; and when the "A" field does not equal "LIFE", the "B" field must NOT be populated.

 

I have tried the following, but it is not working:

(cfValues['A']*.value.contains('LIFE') && cfValues['B']) || ( ! cfValues['A']*.value.contains('LIFE') && ! cfValues['B'])

 

Thank you so much for your assistance!

2 answers

1 accepted

0 vote

Just this should suffice:

cfValues["A"]*.value.contains("LIFE") ^ ! cfValues["B"]

untested. Uses exclusive or, ie the value is LIFE, xor B is empty. Only one of those must be true, not neither, not both.

I made a couple of edits because I couldn't visualise it... try that, if it doesn't work negate the second part.

 

 

 

 

Jamie,

Many thanks for the promptness of your reply and your working solution (what does the "^" character represent?), which leads to a follow-on question:

 

How would you code the following:

When the multi-select "A" field equals "LIFE", the "C", "D", and "E" fields must be populated; and when the "A" field does not equal "LIFE", the "C", "D", and "E"  fields must NOT be populated.


(cfValues["A"]*.value.contains("LIFE") &&  cfValues['C'] &&  cfValues['D'] &&  cfValues['E'] ) || 

(! cfValues["A"]*.value.contains("LIFE") && ! cfValues['C'] && !  cfValues['D'] && ! cfValues['E'] )


Thank you again for your professional assistance!

 

Something like this: def isNotLife = ! ("LIFE" in cfValues["A"]*.value) def otherFieldsHaveValue = ["C", "D", "E"].every {cfValues[it] as Boolean} isNotLife ^ otherFieldsHaveValue although you could write it different ways. > what does the "^" character represent? I said in my answer, it's xor - http://www.groovy-lang.org/operators.html#_bitwise_operators. Not that that page is so helpful actually.

Jamie, Excuse me for my ignorance. I have tried the script below, but it does not work for my following use case: When the multi-select "A" field equals "LIFE", the "B", "C", "D", and "E" fields must be populated; and when the "A" field does not equal "LIFE", the "B", "C", "D", and "E" fields must NOT be populated. cfValues["A"]*.value.contains("LIFE") ^ ! [cfValues["B"], cfValues["C"], cfValues["D"], cfValues["E"]] Do you have any remaining ideas. Thank you again for your assistance.

OK let's try the more long-winded but clearer way: def isLife = ("LIFE" in cfValues["A"]*.value) if (isLife) { if (cfValues["A"] && cfValues["B"] && cfValues["C"]) { return true } return false } else { if (! (cfValues["A"] && cfValues["B"] && cfValues["C"])) { return true } return false }

I got it wrong again, it should be: def isLife = ("LIFE" in cfValues["A"]*.value) if (isLife) { if (cfValues["A"] && cfValues["B"] && cfValues["C"]) { return true } return false } else { if (cfValues["A"] || cfValues["B"] || cfValues["C"]) { return false } return true }

Jamie, You are a very kind man for continuing to work to assist me. I have tried the following but it still does not work. def isLife = ("LIFE" in cfValues["A"]*.value) if (isLife) { if (cfValues["A"] && cfValues["B"] && cfValues["C"] && cfValues["D"] && cfValues["E"]) {return true } return false } else { if (cfValues["A"] || cfValues["B"] || cfValues["C"] || cfValues["D"] || cfValues["E"]) {return false } return true } As previously stated, the script needs to meet these conditions: When the multi-select "A" field equals "LIFE", the "B", "C", "D", and "E" custom fields must be populated; and when the "A" field does not equal "LIFE", the "B", "C", "D", and "E" custom fields must NOT be populated. Also, please advise me on the procedures for submitting you for Knighthood, but be aware that my budget is very limited.

Hi Jeff... still waiting for the telegram from the old girl... one day. OK so... I'm pretty confident my example was right. The problem might be that B, C, D etc need to be the actual *names* of your custom fields, including any whitespace and punctuation. People often accidentally add a trailing space which would then need to be present in the script. In your pseudo code example, you still have "A" in the bit after "def isLife"... which is wrong, you should not check that field there. That was also in my example, which was a mistake, but it's a bit confusing to work with A, B, C etc. Below is updated code. If this doesn't work I recommend you to create a ticket at https://productsupport.adaptavist.com/servicedesk/customer/portal/2 with the *real* code. def isLife = ("LIFE" in cfValues["A"]*.value) if (isLife) { if (cfValues["B"] && cfValues["C"] && cfValues["D"] && cfValues["E"]) { return true } return false } else { if (cfValues["B"] || cfValues["C"] || cfValues["D"] || cfValues["E"]) { return false } return true }

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 Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

116 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