Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Scriptrunner Behavior to find 'None' value

Daniel Steffano July 19, 2022

Hey there,

I'm working on a script that detects the field values and reacts accordingly, but I'm running into an issue where I can't accurately determine if a multi-list selection is equal to the default option, "None". The same is true even after selecting a different item, then clicking back to "None". I am able to get my code to react to the values selected otherwise.

The code is executing under the myVariable field. Any help is greatly appreciated - code below.


def myVariable = getFieldById("customfield_15201")
String myVariableValue = (String) myVariable.getValue()

 

// I've tried all of the following and none of these work.

if (myVariable.contains('None')) { ... }
if (myVariable.contains(null)) { ... }
if (myVariable.equals('')) { ... }
if (myVariable.equal('None')) { ... }
if (myVariable.equals(null)) { ... }
if (myVariable.isEmpty()) { ... }
if (myVariable.isBlank()) { ... }

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
3 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 19, 2022

Your error is with 

String myVariableValue = ( String ) TeleportField .getValue()

In my experience, it's never a good idea to perform this sort of type conversion in groovy.

A multi-select field should typically return an array. If you convert that to strings, it becomes complex to process it.

Let groovy do the work for you.

def myField = getFieldById("customfield_15201")
def myFieldValue = myField.value
if(!myFieldValue){
//the field is not populated, that could be because it returns one of the followin
if(myFieldValue == null){
log.info "field is null"
}
if(myFieldValue instanceof String){
log.info "field is an empty string"
}
if(myfieldValue instanceof List){
log.info "field is an empty array" //this is the only one that should output on a multi-select field
}

} else {
if(myfieldValue instanceof List){
//bonus, if you have values in your multi-select
myFieldValue.each{selectedOption->
log.info "label of the selected option is: $selectedOption"
}
//or if you want the optionIds
myfield.formValue.each{optionId->
log.info "id of the selected option is: $optionId"
}
}
}


Daniel Steffano July 22, 2022

Thanks, this helped me get to where I wanted to go.

Like Deleted user likes this
TAGS
AUG Leaders

Atlassian Community Events