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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,464,074
Community Members
 
Community Events
176
Community Groups

Scriptrunner Behavior to find 'None' value

Edited

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

2 votes
Answer accepted

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"
}
}
}


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

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events