I have written a post function script to check the value of a multi-select list and when I check the field value, I see the option I want but when I attempt to use the contains() or any.equals() methods to validate this in an if/else statement, I am getting a false even when the value should throw a true.
Please find my script below:
import java.util.ArrayList
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
IssueManager issueManger = ComponentAccessor.getIssueManager()
MutableIssue curEpicIssue = issue
CustomFieldManager cfManager = ComponentAccessor.getCustomFieldManager()
CustomField productLineField = cfManager.getCustomFieldObject('customfield_17003')
ArrayList<String> productLineFieldValue = curEpicIssue.getCustomFieldValue(productLineField) //Multi select list field
log.warn("Current Issue " + curEpicIssue)
log.warn("Current Issue's issue type " + curEpicIssue.getIssueType().getName())
log.warn("Value from the Product Line Field " + productLineFieldValue)
log.warn("Class of the Product Line Field " + productLineFieldValue.getClass())
log.warn("Size of the Product Line Field " + productLineFieldValue.size())
log.warn("Does the Product Line field contain the value Others? " + productLineFieldValue.contains("Others")) //any().toString().equals("Others"))
log.warn("Does the Product Line field contain the value identity? " + productLineFieldValue.contains("Identity"))
if(productLineFieldValue){
productLineFieldValue.each {
log.warn("The following values are contained in the Product Line field's array: $it.value")
}
}
and the result returned is below
2024-05-14 10:29:04,367 WARN [runner.ScriptBindingsManager]: Current Issue OPM-15035
2024-05-14 10:29:04,367 WARN [runner.ScriptBindingsManager]: Current Issue's issue type: Epic
2024-05-14 10:29:04,367 WARN [runner.ScriptBindingsManager]: Value from the Product Line Field: [Others]
2024-05-14 10:29:04,367 WARN [runner.ScriptBindingsManager]: Class of the Product Line Field: class java.util.ArrayList
2024-05-14 10:29:04,367 WARN [runner.ScriptBindingsManager]: Size of the Product Line Field: 1
2024-05-14 10:29:04,368 WARN [runner.ScriptBindingsManager]: Does the Product Line field contain the value Others? false
2024-05-14 10:29:04,368 WARN [runner.ScriptBindingsManager]: Does the Product Line field contain the value identity? false
2024-05-14 10:29:04,369 WARN [runner.ScriptBindingsManager]: The following values are contained in the Product Line field's array: Others
From what I see, it contains my expected value but I still get a false when checking if it contains the value others.
For your requirement, I suggest changing your approach and use ScriptRunner's HAPI feature to simplify your code.
So in your case, if you intend to perform a validation on the values selected, you can try something like:
def multiSelectValue = issue.getCustomFieldValue('Multi-Select') as String //change the field name
if (multiSelectValue.contains('Value1') || multiSelectValue.contains('Value2') || multiSelectValue.contains('Value3') ) { //change the values
multiSelectValue.each {
log.warn("The following values are contained in the Product Line field's array: $it.value")
}
}
Please note that the sample working code above is not 100% exact to your environment. Hence you will need to make the required modifications.
I hope this helps to solve your issue. :-)
Thank you and Kind regards,
Ram
This part is incorrect:
ArrayList<String> productLineFieldValue = curEpicIssue.getCustomFieldValue(productLineField)
It's not a list of String values, it's a list of LazyLoadedOption (https://docs.atlassian.com/software/jira/docs/api/9.4.18/com/atlassian/jira/issue/customfields/option/LazyLoadedOption.html)
It shows values in log because of it's toString method:
public String toString() {
return this.getValue();
}
Try switching this:
log.warn("Does the Product Line field contain the value Others? " + productLineFieldValue.contains("Others"))
To:
log.warn("Does the Product Line field contain the value Others? " + productLineFieldValue.stream().anyMatch(v -> v.getValue().equals("Others")))
Groovy-wise you could convert the lazyloadedoptions to string using for example
List<String> actualStringValues = curEpicIssue.getCustomFieldValue(productLineField)*.getValue()
Keep in mind though that Issue#getCustomFieldValue() can return null - so to avoid NPE's I'd recommend something similar to:
List<LazyLoadedOption> productLineFieldValue = (List<LazyLoadedOption>) curEpicIssue.getCustomFieldValue(productLineField)
List<String> productLineFieldStringValues = productLineFieldValue != null ? productLineFieldValue.stream().map(v -> v.getValue()).collect(Collectors.toList()) : Collections.emptyList()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks a lot @Radek Dostál .
I will give this a shot. I had assumed that the options would be strings which now thinking of it makes no sense.
I will make the necessary adjustments and keep you updated.
@Ram Kumar Aravindakshan _Adaptavist_, I am not fully used to Hapi hence I went with the classic groovy/java approach which I am more conversant with.
I will have a look at your suggestion.
Thanks again.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks a lot both of you.
I went with @Ram Kumar Aravindakshan _Adaptavist_ solution as it was the faster fix for the issue.
Thanks again both of you.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.