Scriptrunner behavior

Ashish K July 27, 2020

Hi All,

I have a checkbox field call ENV with the values = NonProd, Stage, Prod.

If anyone selected Prod or Stage from the list we need to make the Security field required.

 

Here is the code I tried and wont work if I select multiple values in the ENV field, please let me know if anything I can modify in this.

import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue

def RE = getFieldById("customfield_14200")
def SF = getFieldById("customfield_14217")

def RE1 = RE.value.toString()
if (RE1 in ["Prod" , "Stage"] {
SF.setRequired(true)
}
else {
SF.setRequired(false)
}

Jira Server 7.12.3

Scriptrunner version 5.4.45

TIA!!

1 answer

1 accepted

1 vote
Answer accepted
Italo Qualisoni [e-Core]
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 27, 2020

I think your issue is that when your checklist has more than one value, you are receiving an array with 2 items instead of an array with one single element.

Can you check if this would work?

def RE = getFieldById("customfield_14200")
def SF = getFieldById("customfield_14217")

if(RE.getValue()*.contains("Prod") || RE.getValue()*.contains("Stage") ){
SF.setRequired(true)
}
else {
SF.setRequired(false)
}

 This will set SF as required if the checkbox has Prod or Stage or both selected.

Ashish K July 27, 2020

Hi @Italo Qualisoni [e-Core] , thank you, this what I see now on line 5.Snag_6f0c98b.png

 

If I use the below script, even if I select non prod this script makes the other field required.

def RE = getFieldById("customfield_14200")
def SF = getFieldById("customfield_14217")

def RE1 = RE.value.toString()
if(RE1.contains("Prod") || RE1.contains("Stage") ){
SF.setRequired(true)
SF.setHidden(false)
}
else {
SF.setRequired(false)
SF.setHidden(true)
}

Italo Qualisoni [e-Core]
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 27, 2020

@Ashish K 

Try the following, when you select one value the instance type is String and when you have more than one value in your checkbox your instance is a List<Option>

import com.atlassian.jira.issue.customfields.option.Option

def RE = getFieldById("customfield_14200")
def SF = getFieldById("customfield_14217")

def options = RE.getValue()

//Single option selected
if(options instanceof String){
if(["Prod","Stage"].contains(options)){
SF.setRequired(true);
}
else{
SF.setRequired(false);
}
}
//Multiple options
else if(options instanceof List<Option> ){
if(options.contains("Prod") || options.contains("Stage") ){
SF.setRequired(true);
}
else{
SF.setRequired(false);
}
}
Ashish K July 27, 2020

Thank you so much @Italo Qualisoni [e-Core] 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
7.12.3
TAGS
AUG Leaders

Atlassian Community Events