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,456,969
Community Members
 
Community Events
176
Community Groups

How to get the value from Select List (multiple choices) using script post-function

I found many answers in community here to get value from Select List (multiple choices) by using script post-function (ScriptRunner), but I'm still can't get it during if-else statement.

For example, using issue.getCustomFieldValue(), I can get the value of the selected list, it showed "Field Value: [Environmental Services]" in log, but it can't went through during if-else statement as username shows null.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField

MutableIssue issues = issue
CustomField businessCat = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_23603")
String userName

log.warn("Issue: " + issue)
log.warn("businessCat Field: " + businessCat)

//def businessCatOptions = ComponentAccessor.optionsManager.getOptions(businessCat.getRelevantConfig(issue))
def businessCatValue = issue.getCustomFieldValue(businessCat)
log.warn("Field Value: " + businessCatValue)

if (businessCatValue == "Industrial Manufacturing and Processing Machinery and Accessories") {
userName = "jarevalo"
}
else if (businessCatValue == "Environmental Services") {
userName = "rasoni"
}

log.warn("username: " + user)

 

Example using ComponentAccessor.optionsManager.getOptions(), businessCatOptions get all the options from the Select List (multiple choices), but i don't know how to get the specific value such as "Industrial Manufacturing and Processing Machinery and Accessories" or "Environmental Services"

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField

MutableIssue issues = issue
CustomField businessCat = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_23603")
String userName

log.warn("Issue: " + issue)
log.warn("businessCat Field: " + businessCat)

def businessCatOptions = ComponentAccessor.optionsManager.getOptions(businessCat.getRelevantConfig(issue))
//def businessCatValue = issue.getCustomFieldValue(businessCat)
log.warn("Options: " + businessCatOptions)

if (businessCatOptions.findAll{it.value == "Industrial Manufacturing and Processing Machinery and Accessories"}) {
userName = "jarevalo"
}
else if (businessCatOptions.findAll{it.value == "Environmental Services"}) {
userName = "rasoni"
}

log.warn("username: " + user)

Any solutions or suggestions to recommend for me to implement? 

Thanks

2 answers

1 accepted

0 votes
Answer accepted

When exploring something like this, I helps to examine the results with a bit more details.

Instead of 

log.warn("businessCat Field: " + businessCat)

You can do

log.warn "Field Value: $businessCatValue ($businessCatValue.getClass()}"

It should print something like this in the log

Field Value: [Environmental Services] (ArrayList)

Ah, so, we have an array... lets look what we have inside of it by examining the first element (index 0)

log.warn "Field Value: ${businessCatValue[0]} ($businessCatValue[0].getClass()}"

Now, it prints:

Field Value: Environmental Services (LazyLoadedOption)

Notice the square brackets are gone.

From there you can have a look at the  Javadoc for LazyLoadedOption for what methods are available. You'll see that getValue returns a string. Perfect, that's what you want to compare.

So using some groovy we can convert your whole script to:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.Option

def businessCatUsernameMap = [
'Industrial Manufacturing and Processing Machinery and Accessories':'jarevalo',
'Environmental Services':'rasoni'
]
issue = issue as MutableIssue
def businessCat = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_23603")
def businessCatValue = issue.getCustomFieldValue(businessCat) as List<Option>
def userName = businessCatUsernameMap.find{it.key in businessCatValue*.value}.value

log.warn "Issue: $issue.key"
log.warn "businessCat values: $businessCatValue.value"
log.warn "username: $userName"

 The biggest piece of "groovy magic" is in here

businessCatUsernameMap.find{it.key in businessCatValue*.value}.value

This will find the FIRST businessCat in the map that is included in the list of selected options and return the associated username value.

This will make it easy to add additional businessCat/username mapping in the future.

Hey @Peter-Dave Sheehan 

Thanks for your guide, your method works for me and good explanation, well understanding for me.

Thanks a lot

Martin

The first part sounds like this:

def businessCatValue = issue.getCustomFieldValue(businessCat)
if (businessCatValue == "Industrial Manufacturing and Processing Machinery and Accessories")

 

The 'businessCatValue' in this case is a https://docs.atlassian.com/software/jira/docs/api/8.20.8/com/atlassian/jira/issue/customfields/option/LazyLoadedOption.html

Equals will not work, because internally it will only result in true if you are comparing it with another LazyLoadedOption, and both of the objects will be compared by their ID. So String comparison won't work unless you do

businessCatValue.getValue() == "my value"

Note that you might be getting static type checking errors on "getValue()". This is because the STC does not know the real class. You can get around that with

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

// SINGLE SELECT
LazyLoadedOption businessCatValue = (LazyLoadedOption) issue.getCustomFieldValue(businessCat)
if (businessCatValue != null && businessCatValue.getValue() == "my value") {
..
}
// MULTI SELECT
List<LazyLoadedOption> businessCatValue = (List<LazyLoadedOption>) issue.getCustomFieldValue(businessCat)
if (businessCatValue != null && businessCatValue.find { it.getValue() == "my value" } != null) {
..
}

 

Edit: added comparisons to both single/multi select examples

Hey @Radek Dostál 

Thanks for your guide too, the comparison makes sense to me, but I will try out your method too so I can have two method in the future.

 

Martin

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events