How to set custom field mandatory based on select list custom field option?

Serj Shcherbakov July 29, 2015

Hi!

Can we set a custom field as mandatory based on another custom field value at issue creation stage? (with  Simple scripted validator)

I Have:

"customfield_17212" (Select List) with optioins: 23742, 23745, 23749, 23753 - if filled (condition).

"customfield_10355" (Select List) - must be required.

I think this script can help me, but, it is not working.

import com.atlassian.jira.ComponentManager

FormField field1 = getFieldById("customfield_17212")
FormField field2 = getFieldById("customfield_10355")
if(field1.getValue().equals("23742","23745","23749","23753"))
{ field2.setRequired(true) }
else { field2.setRequired(false) }

Please help me,
Sergey. 

2 answers

1 accepted

2 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.
July 29, 2015

That code above is for behaviours.

If you are using simple scripted validator, the code might be

(! cfValues["Name of first select"]*.value.intersect(["val1", "val2", "valn"])
 || cfValues["Second select"]

So to translate that, if the selected value in the first field is not one of the ones you care about, then fine, if it is then the other field must have a value.

A SSV is a simpler and more reliable method than using behaviours, the advantage of behaviours is that the second field becomes required as soon as they change the first field.

Note I'm using field names and field values, not field IDs and option IDs. This makes it more portable.

 

0 votes
Serj Shcherbakov July 29, 2015

Hi Jamie,
I'll tried to modify your example like this:

(! cfValues["customfield_17212"]*.value.intersect(["23742", "23745", "23749", "23753"])
 || cfValues["customfield_10355"]


then like this:

(! cfValues["SOW type"]*.value.intersect(["New Solution", "POC/ Prototype development", "SDN/NFV POC", "Upgrade"])
 || cfValues["TOMS Modules"]

But, it still doesn't work for me.

5543.jpg

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.
July 29, 2015

SOW Type is a multiselect right? It's the second one you should be using. What versions of jira and the plugin are you using?

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.
July 29, 2015

If SOW type is a single select use: cfValues["SOW type"].value (ie remove the asterisk). Also pay attention to case of the field names.

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.
July 30, 2015

Sorry, should be ! cfValues["SOW type"].value in ["New Solution", "POC/ Prototype development", "SDN/NFV POC", "Upgrade"] || cfValues["TOMS Modules"]

Serj Shcherbakov July 30, 2015

Jamie, many thanks you, but we still need your recomendations. 1. SOW type and TOMS Modules is Select List (multiple choices) 2. A new scrip ignore SOW type values and always alert the TOMS Modules is Required. 3. We use Jira v. 6.2.3 and Script Tunner v.2.1.17 I hope we solve this issue. Thanks for your co.

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.
July 30, 2015

Alright I have actually tested this: if (cfValues["MultiSelectA"] && cfValues["MultiSelectA"]*.value.intersect(["AAA", "BBB"])) { if (! cfValues["Subcomponent"]) { return false } } true

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.
July 30, 2015

note - change field names and values, obviously.

Serj Shcherbakov August 2, 2015

Jamie, this working fine! Thanks you so much!

Suggest an answer

Log in or Sign up to answer