How to Create a Simple Scripted Validator?

Jeff Peters September 21, 2015

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 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.
September 21, 2015

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.

 

 

 

 

0 votes
Jeff Peters September 22, 2015

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!

 

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.
September 22, 2015

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.

Jeff Peters September 23, 2015

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.

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.
September 23, 2015

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 }

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.
September 24, 2015

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 }

Jeff Peters September 24, 2015

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.

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.
September 24, 2015

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
TAGS
AUG Leaders

Atlassian Community Events