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
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.
Thanks for your guide, your method works for me and good explanation, well understanding for me.
Thanks a lot
Martin
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.